I am new to rails and am trying to get my associations straight. I
have a User model and a Property model. I'd like to track who has
updated a property record, so I added an integer field to the
properties table called updated_by. Then I made the following
changes:
In the User model:
has_many :properties
In the Property model:
belongs_to :user, :class_name => "User", :foreign_key =>
'updated_by'
NoMethodError: undefined method `name' for 1:Fixnum
from (irb):5
Yeah the fixnum error is because updated_by is just an integer field to
rails.
If you tried:
prop.user = usr
prop.user.name #=> "Sam Shartles"
This should get you are trying to acheive:
In the User model:
has_many :properties, :foreign_key => 'updated_by'
In the Property model:
belongs_to :updated_by, :class_name => 'User', :foreign_key =>
'updated_by'
I must admit in the past I've tended to use 'updated_by_id' instead as
your field name that way you can access both updated_by_id (returns int)
and updated_by (returns user).