I have an application running in Heroku. The app has a User model with email, password, a few other columns. I recently added more columns to the database locally, including a random token column that is a unique token for each user. When a user is created, the following code is run in the user model:
before_create :set_referer_url
...
private
def set_referer_url
begin
self.referer_url = rand(36**8).to_s(36)
end while User.exists?(:referer_url => self.referer_url)
end
For any new user that is created, they will have this unique token. However, older users will not. I wanted to push this migration (with the new database column) to heroku. How do I add this token for users created before this new column was added. I’ve heard a lot about rake, but any help would be greatly appreciated.
As an aside, I tried to add a token for a test user locally from the rails console by doing the following:
User.find_by_email('xyz').set_referer_url
This, however, resulted in a NoMethod Error: private method. How can I invoke private methods in rake? What would the entire rake procedure look like in order to set up old users with the populated new token column?
To call a private method from outside of the object, use
send:It’s generally bad practice to do this as a matter of course, as private methods are private for a reason. But for a one-off backfill of existing data as part of a migration I think it’s OK.