Mass downcase a field for all records in rails
rails update multiple records at once
rails update multiple records with callbacks
rails bulk update
rails update_all multiple columns
rails update column
update all associated records rails
rails set column value
When I first implemented a User model , I allowed the user to type upper or lowercase email for their login info. The problem is that its a mobile app, and sometimes autocaps happen so the user would not get authenticated. I've changed the CREATE method to downcase the email first. However, this causes people with existing accounts to not be consistent
So how can I add a migration to mass update the email field in the users table to downcase it?
The simplest solution is to just use Ruby code:
class DowncaseEmail < ActiveRecord::Migration def up User.all.each do |user| user.update_attributes :email => user.email.downcase end end end
As others have noted, this is not the most efficient solution. I tend to prefer portability over performance, but that depends on the number of records we are talking about.
A more complex but still portable solution depends on some db specific functions:
class DowncaseEmail < ActiveRecord::Migration def up if %w[MySQL PostgreSQL].include? ActiveRecord::Base.connection.adapter_name execute "UPDATE users SET email = LOWER(email)" else User.all.each do |user| user.update_attributes email: user.email.downcase end end end end
Both Postgres and MySQL support the
LOWER function. SQLite also supports it, but only for ascii encoding which is probably fine for emails, but might cause bugs (since some emails may contain other characters). Rails also potentially supports any number of databases, so relying on these functions can make your app locked in to a specific DB.
Nature, PRELIMINARY record of experiments made with coal dust, and other work of those of Faraday and Lyell (1884): “ There is every reason to believe that much coal $MA Lu can New -- of chapter iii. the committee says:– “As has already been occupied the field in the LC ***ws: - -——1 - : “Record of the First Series of the Rails Migration/Copy/Merge Tool. GitHub Gist: instantly share code, notes, and snippets.
The most efficient way would be to avoid using a Ruby iterator, and do it directly in SQL.
Inside of a normal migration file you can use this SQL for MySQL:
execute("UPDATE users SET email = LOWER(email)")
Financial World, The world, H staggering under its burden of doubt, downcast at the darkening of and a message of hope to the masses of the people of the nations concerned that to easement of the financial burdens of all countries at last has been opened. <-s: ThE NEW WORLD -De Mar in the "laladelphia Record and the farmers #frozen_string_literal: true: module ActiveRecord: module Calculations # Count the records. # Person.count # # => the total count of all people # Person.count(:age) # # => returns the total count of all people whose age is present in database
You can simply go with
User.update_all('email = lower(email)')
English Mechanics and the World of Science, They will be glad to send to other societies at any time when called on to do so. Articles may be sent to Mrs. Kate C. Miner, president of the Alliance Branch, Montague, Mass., Box 6. No mission field of the denomination offers more attractive opportunities for The insects gathered upon the rails in such quantities that the In some cases, you might want to perform a mathematical calculation to set a field value for a single record or even all records. You can perform simple as well as advanced calculations on all or selected records. In addition, you can calculate area, length, perimeter, and other geometric properties on fields in attribute tables.
Never worry about optimizing something that you're only going to do once. Unless you have massive numbers of users, just use the methodology that will be most familiar to you. (Though I would recommend just running the SQL query.)
Find records where field value is all LOWERCASE, In a Rails 3.2 app I have a Tag model and want to find all records where the value in the name:string field is all lowercase. Thus the activerecord query (on Listed below is a look at the updated event-records for the 2014 Mass. All-State Meeting, scheduled for this Saturday, June10, at Bridgewater State College. Like some of the Divisional Records, a few of the sprint races and relays need a bit more research to unearth correct FAT that should be listed in the records section. I have added World Jr. Records as well as a few years of previous
Rather than doing iterations, below is the simplest solution:
ActiveRecord::Base.connection.execute "UPDATE users SET email = LOWER(email)"
Harper's Weekly, M.A.. (Joints. Hopkins). TO ENLIGHTEN An interesting Religious Movement and What it another is gloomy and downcast, no matter what the temptation to be gay; The ordinary phenomena and experiences of life pain them. , Every man the open fields and to sleep under the blue vault of heaven—is a claustrophobe. fields_for will iterate over these three records and create inputs for them. Now no matter how few or many tasks a user has, there will always be three blank text fields for new tasks to be added.
update_all (ActiveRecord::Relation), Updates all records in the current relation with details given. Customer.update_all wants_email: true # Update all books with 'Rails' in their title Note that ActiveRecord will not update the timestamp fields (updated_at/updated_on) when Active Record BasicsThis guide is an introduction to Active Record.After reading this guide, you will know: What Object Relational Mapping and Active Record are and how they are used in Rails. How Active Record fits into the Model-View-Controller paradigm. How to use Active Record models to manipulate data stored in a relational database. Active Record schema naming conventions. The concepts
Active Record Migrations, the Rest of Our Forms · Parameter Naming Conventions and Mass Assignment Fortunately, there's an easy way of dealing with this in Rails, because Active a model_names table; column names for foreign keys should be the lowercase, should be using db:schema:load, not running all the migrations # from scratch. We search through court records dealing with all cases related to family and domestic relationships of the person. Traffic Court Cases Find his/her court cases that concern minor and major violations of various traffic laws, such as Driving Under Influence, Speeding, Aggressive Driving, Hit & Run, and many more.
Railway Age and Railway Review, Besides the usual statements, it gives a list of all the railroads in the state, with an hour and a half, being ihe second largest catastrophe of the kind on record. that the face of the torrent was a mass of timber and trees, no water being visible. topers gazed with downcast mien on the destruction of tbe invigorating liquid. Reassess the need for using bed rails on a frequent, regular basis. To report an adverse event or medical device problem, please call FDA’s MedWatch Reporting Program at 1-800-FDA-1088.
- Email addresses are case-insensitive, so they should always be folded to lower-case for comparison and storage. It's one of those things that gets overlooked during app design... once... then you remember it ever after. :-)
- Not true. Email addresses ARE case-sensitive. RFC5321 states: [The local-part of a mailbox MUST BE treated as case sensitive. Therefore, SMTP implementations MUST take care to preserve the case of mailbox local-parts. In particular, for some hosts, the user "smith" is different from the user "Smith".] That said, for practical reasons, I agree that downcasing email addresses, particularly for comparisons, is worth doing.
- Usually there will be some code that needs to be changed in addition, so the comparisons occur in lower-case, and newly inserted records are folded to lower-case to keep the problem from reoccuring.
- love this solution.
- Thanks Amir! works!
- Thanks everyone for solutions. I only had about 300 users, so just doing a simple migration was easiest for me. Cheers.