1

我有 2 个模型 (model1model2) 具有多对多关系。一个表已经存在,它只包含每个模型的主键。这些has_many through关系是在它们各自的模型中建立的。但是,当我在 POST 中尝试以下操作时,我得到了经典

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不是零。 model1.model2s不是零。 model2不是零。这个错误来自哪里?

4

1 回答 1

1

如果您有一个包含每个模型的主键的连接表,那么您has_and_belongs_to_many不希望has_many :through

于 2012-06-28T21:21:53.063 回答