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

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

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

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

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


Josh S.
http://blog.hasmanythrough.com

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