Reputation: 39
DB table User with a column name uid, create a method uid in the User model. When you submit a form with the field of :uid, it calls the User method uid. I had never seen this explained anywhere and no idea Rails did this.
Is this a good way to put in logic for that column, or should this be avoided?
Upvotes: 0
Views: 1765
Reputation: 2483
It uses the so called "Ghost method" pattern in couple with the "Dynamic method" pattern, by using the method_missing
callback.
See here the explanation: http://rubylearning.com/blog/2010/10/07/do-you-know-rubys-chainsaw-method/
Upvotes: 1
Reputation: 7066
It's a great way to add logic to your model. Read more about it here:
http://api.rubyonrails.org/classes/ActiveRecord/Base.html
under "Overwriting default accessors".
Upvotes: 1