ssstratton
2006-Sep-27 15:36 UTC
Rails newbie - can anyone please help with many to many NoMethodError
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Gernot Kogler
2006-Sep-27 15:50 UTC
Re: Rails newbie - can anyone please help with many to many
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
ssstratton
2006-Sep-27 19:53 UTC
Re: Rails newbie - can anyone please help with many to many
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Josh Susser
2006-Sep-28 05:00 UTC
Re: Rails newbie - can anyone please help with many to many
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 StrattonIf 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 Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
ssstratton
2006-Sep-28 09:48 UTC
Re: Rails newbie - can anyone please help with many to many
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---