I have 2 important entities and an association table that is created with foreign keys
class Assoc << ActiveRecord::Base
belongs_to entity1
belongs_to entity2
end
class Entity1 << ActiveRecord::Base
has_many :assocs
has_many :entities2 :through=>:assocs
end
class Entity2 << ActiveRecord::Base
has_many :assocs
has_many :entities1 :through=>:assocs
end
I saw this question how to add records to has_many :through association in rails and it seems reasonable to me, except for the fact that it seems that he can create an instance of Assoc without having Entity1 and Entity2 assigned for it. This doesn't seem to be my case.
In my specific case I am dealing with Properties and Property Facilities. The assoc table will only link a property with many property facilities and vice versa. Assoc is defined by 2 fks, one for each entity.
Once I submited a form, I'm in the action now. I already got the Property
and stored it in @property
. Now I want to add the facilities that are in params[:property][:facility_ids]
(its an array of ids).
What should I do?