user8745303
user8745303

Reputation: 39

Rails, Column name and method name behavior

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

Answers (2)

Andrea Salicetti
Andrea Salicetti

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

M. Cypher
M. Cypher

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

Related Questions