0

I've set up a has_and_belongs_to_many association between two models. I need to access an attribute from one of the models for a method in another model. Right now my code looks like this, but I'm getting an error saying 'undefined local variable 'model1_id''

Model_2.rb

Class Model_2 < ActiveRecord:: Base
...
has_and_belongs_to_many :model_1

def some_method
   attr_1 * Model_1.find(model_1_id).attr_I_need
end

What am I missing? Thanks!

sacshu
  • 397
  • 9
  • 20

1 Answers1

0

First of all, you should write

 has_and_belongs_to_many :model_1s 

in Model_2 if your model name is Model_1, look carefully at last s in model_1s, lly write

 has_and_belongs_to_many :model_2s

in Model_1

and it seems that, your local method can not find model_1_id variable.

have you defined it some where ??

maximus ツ
  • 7,949
  • 3
  • 25
  • 54
  • Right, okay so I've got the plural 's' for both models. But I'm not sure what you mean by if I've defined the ID somewhere. Where would I define it, isn't it already created by the model when the migration happens? – sacshu Sep 04 '12 at 18:19
  • No, you have to create on your own in join table in your case. Have you created join table for this ? {Actually I was asked to check local variable that you passed to your local method, but you also need to create join table} – maximus ツ Sep 04 '12 at 18:22
  • Oh yeah, I have created the join table, so its got Model_1_id and Model_2_id and an index on them, but still no luck – sacshu Sep 04 '12 at 18:29