In my application I have 2 classes like this:
class City < ActiveRecord::Base
has_many :events
end
class Event < ActiveRecord::Base
belongs_to :city
attr_accessible :title, :city_id
end
If I create city object:
city = City.create!(:name => 'My city')
and then pass parameters to create event like this:
event = Event.create!(:name => 'Some event', :city => city)
I get
event.city_id => null
So the question is – is it possible to pass parameters in such a way to get my objects connected, what am I doing wrong? Or should I use other ways (like
event.city = city
)
?
Generally this happens when you have an
attr_accessorthat excludes or anattr_protectedthat includes the:cityattribute onEvent. Allowing:city_idto be accessible does not automatically allow:cityto be so.(NB: this answer provided as per the discussion in comments above, and thus community-wiki.)