0

I have two models - Question & Tag.

Both have a HABTM association between themselves. There is also a questions_tags join table.

If I have a question (q), I can find the tags by simply doing q.tags.

But, if I have a tag (t), when I try to go the other way (t.questions), I get an error like this:

NoMethodError: undefined method `questions' for #<ActiveRecord::Relation:0x007fda147522b8>

Is HABTM not supposed to work both ways? How do I get the questions associated with a tag in my case?

marcamillion
  • 32,933
  • 55
  • 189
  • 380
  • 1
    can you show your code where you use `t.questions`. I doubt that t is not a Tag but an AR::Relation as the error suggest. something like `t = Tag.where(id: some_id)` (forgot to add `.first`) which will cause the error – jvnill Mar 11 '13 at 09:00
  • Ahhh.....you are right. It was returning an array....not the individual object. If you add that as the answer, I will accept it. – marcamillion Mar 11 '13 at 09:01

1 Answers1

1

I doubt that t is not a Tag object but an AR::Relation as the error suggest. Something like t = Tag.where(id: some_id) (forgot to add .first) which will cause the error. :)

jvnill
  • 29,479
  • 4
  • 83
  • 86