I have 2 models (model1 and model2) that have a many to many relationship. A table already exists which only contains the primary key from each model. The has_many through relationships are set up in their respective models. However, when I try the following in a POST, I get the classic
NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array. The error occurred while
evaluating nil.[]":
model1 = Model1.find(params[:id])
model2 = Model2.find(params[:model2_id])
model1.model2s << model2
model1 is not nil. model1.model2s is not nil. model2 is not nil. Where is this error coming from?
If you have a join table that only has the primary keys from each of your models then you want
has_and_belongs_to_manynothas_many :through