# Rails newbie - can anyone please help with many to many NoMe

**URL:** <https://www.ruby-forum.com/t/rails-newbie-can-anyone-please-help-with-many-to-many-nome/70236>\
**Category:** Rails\
**Created:** [September 27, 2006, 5:45pm UTC](https://www.ruby-forum.com/t/rails-newbie-can-anyone-please-help-with-many-to-many-nome/70236 "2006-09-27T17:45:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ssstratton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b487fb/32.png) [@ssstratton](https://www.ruby-forum.com/u/ssstratton)\
**Post date:** [September 27, 2006, 5:45pm UTC](https://www.ruby-forum.com/t/rails-newbie-can-anyone-please-help-with-many-to-many-nome/70236/1 "2006-09-27T17:45:21Z")

</div>

Please can someone help with the following problem -

I’m using LoginEngine and have a Users table linked many to many to a  
Plants table. The joining table is plants\_users with plant\_id and  
user\_id (there are extra fields, so I’m avoiding  
has\_and\_belongs\_to\_many - plus it didn’t work :().

::user.rb starts:-  
class User \< ActiveRecord::Base  
has\_many :Plants, :through =\> :plants\_users  
…

::plant.rb is:-  
class Plant \< ActiveRecord::Base  
has\_many :Users, :through =\> :plants\_users  
end

::plant\_user.rb is:  
class PlantUser \< ActiveRecord::Base  
belongs\_to :user  
belongs\_to :plant  
end

::plant\_controller.rb is  
class PlantController \< ApplicationController  
before\_filter :login\_required  
scaffold :plant

def list  
@plants = @session[‘user’].plants  
end  
end

So far so good (I think), but when I access this (using instantrails),  
I get  
NoMethodError in PlantController#list  
undefined method `plants’ for #User:0x3969fa0  
So - apparently User has no plants method - but why not - surely thats  
whats rails does?! I’ve tried reading loads of tutorials, changing this  
code umpteen times, using has\_and\_belongs\_to\_many and can’t seem to get  
anywhere.

Please help  
Cheers  
Andy Stratton

---

<div class="post-metadata">

**Author:** ![ssstratton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b487fb/32.png) [@ssstratton](https://www.ruby-forum.com/u/ssstratton)\
**Post date:** [September 27, 2006, 5:50pm UTC](https://www.ruby-forum.com/t/rails-newbie-can-anyone-please-help-with-many-to-many-nome/70236/2 "2006-09-27T17:50:50Z")

</div>

ssstratton wrote:

> ::user.rb starts:-  
> class User \< ActiveRecord::Base  
> has\_many :Plants, :through =\> :plants\_users  
> …

In your user class, you have to write the symbol :plants in lower case,  
like so:

has\_many :plants, :through =\> :plants\_users

---

<div class="post-metadata">

**Author:** ![ssstratton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b487fb/32.png) [@ssstratton](https://www.ruby-forum.com/u/ssstratton)\
**Post date:** [September 27, 2006, 10:00pm UTC](https://www.ruby-forum.com/t/rails-newbie-can-anyone-please-help-with-many-to-many-nome/70236/3 "2006-09-27T22:00:01Z")

</div>

Thanks Gernot - I’m now ‘unstuck’ for the moment. Though - I only  
arranged to get the has\_and\_belongs\_to\_many version to work - but  
that’s better than before.

Cheers  
Andy Stratton

---

<div class="post-metadata">

**Author:** ![ssstratton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b487fb/32.png) [@ssstratton](https://www.ruby-forum.com/u/ssstratton)\
**Post date:** [September 28, 2006, 7:00am UTC](https://www.ruby-forum.com/t/rails-newbie-can-anyone-please-help-with-many-to-many-nome/70236/4 "2006-09-28T07:00:25Z")

</div>

ssstratton wrote:

> Please can someone help with the following problem -
> 
> I’m using LoginEngine and have a Users table linked many to many to a  
> Plants table. The joining table is plants\_users with plant\_id and  
> user\_id (there are extra fields, so I’m avoiding  
> has\_and\_belongs\_to\_many - plus it didn’t work :().
> 
> ::user.rb starts:-  
> class User \< ActiveRecord::Base  
> has\_many :Plants, :through =\> :plants\_users  
> …
> 
> ::plant.rb is:-  
> class Plant \< ActiveRecord::Base  
> has\_many :Users, :through =\> :plants\_users  
> end
> 
> ::plant\_user.rb is:  
> class PlantUser \< ActiveRecord::Base  
> belongs\_to :user  
> belongs\_to :plant  
> end
> 
> ::plant\_controller.rb is  
> class PlantController \< ApplicationController  
> before\_filter :login\_required  
> scaffold :plant
> 
> def list  
> @plants = @session[‘user’].plants  
> end  
> end
> 
> So far so good (I think), but when I access this (using instantrails),  
> I get  
> NoMethodError in PlantController#list  
> undefined method `plants’ for #User:0x3969fa0  
> So - apparently User has no plants method - but why not - surely thats  
> whats rails does?! I’ve tried reading loads of tutorials, changing this  
> code umpteen times, using has\_and\_belongs\_to\_many and can’t seem to get  
> anywhere.
> 
> Please help  
> Cheers  
> Andy Stratton

If you use a PlantUser join model for has\_many :through, you can’t name  
the db table plants\_users, as ActiveRecord will be looking for  
plant\_users (singular plant). The plants\_users convention is for habtm  
only, not for hmt. It’s better to name your join model / table as a noun  
like “assignments” or “subscriptions”.

I have a lot of examples and tips on using hmt and other associations on  
my blog. Here’s one to get you started:  
[http://blog.hasmanythrough.com/articles/2006/04/20/many-to-many-dance-off](http://blog.hasmanythrough.com/articles/2006/04/20/many-to-many-dance-off)

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![ssstratton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b487fb/32.png) [@ssstratton](https://www.ruby-forum.com/u/ssstratton)\
**Post date:** [September 28, 2006, 11:53am UTC](https://www.ruby-forum.com/t/rails-newbie-can-anyone-please-help-with-many-to-many-nome/70236/5 "2006-09-28T11:53:31Z")

</div>

Thanks josh - I have has\_many :through working now.

If anyone is interested, I can post abridged code - though josh’s blog  
covers it nicely anyway.

Cheers  
Andy Stratton
