A House has_and_belongs_to_many Amenities. I have the following tables: House, Amenities_Houses, and Amenity. house_id and amenity_id are the only things in the join table. Why on earth does #House.amenities not work? It''s saying that the method is not defined. Using latest stable gems and rails 0.9.5. Ideas? Thanks, Joe
Your table names should be in lower case: houses, amenities and amenities_houses. That is what Rails expects. Otherwise you have to specify the table names in your model class definitions. If that doesn''t help, could you post your class and table definitions here, so it will be a bit easier to spot what''s wrong. //jarkko On 23.2.2005, at 10:57, Joe Van Dyk wrote:> A House has_and_belongs_to_many Amenities. > > I have the following tables: House, Amenities_Houses, and Amenity. > house_id and amenity_id are the only things in the join table. > > Why on earth does #House.amenities not work? It''s saying that the > method is not defined. > > Using latest stable gems and rails 0.9.5. > > Ideas? > > Thanks, > Joe > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
The table names are all lowercase. class House < ActiveRecord::Base belongs_to :community belongs_to :image has_and_belongs_to_many :amenities has_many :images end class Amenity < ActiveRecord::Base end CREATE TABLE `amenities` ( `id` int(11) NOT NULL auto_increment, `title` varchar(255) NOT NULL default '''', PRIMARY KEY (`id`) ) TYPE=MyISAM AUTO_INCREMENT=4 ; CREATE TABLE `amenities_houses` ( `house_id` int(11) NOT NULL default ''0'', `amenity_id` int(11) NOT NULL default ''0'', PRIMARY KEY (`house_id`,`amenity_id`) ) TYPE=MyISAM; CREATE TABLE `houses` ( `id` int(11) NOT NULL auto_increment, `title` varchar(255) NOT NULL default '''', `price` decimal(10,2) NOT NULL default ''0.00'', `description` longtext NOT NULL, `bedrooms` varchar(5) NOT NULL default ''1.0'', `bathrooms` varchar(5) NOT NULL default '''', `square_footage` varchar(10) NOT NULL default '''', `year_built` varchar(4) NOT NULL default ''0'', `listing_id` varchar(15) NOT NULL default '''', `street_address` varchar(255) NOT NULL default '''', `floors` varchar(11) NOT NULL default ''0'', `image_id` int(11) NOT NULL default ''0'', `community_id` int(11) NOT NULL default ''0'', PRIMARY KEY (`id`) ) TYPE=MyISAM AUTO_INCREMENT=25 ; On Wed, 23 Feb 2005 11:04:30 +0200, Jarkko Laine <jarkko-k1O+Gnc6WpmsTnJN9+BGXg@public.gmane.org> wrote:> Your table names should be in lower case: houses, amenities and > amenities_houses. That is what Rails expects. Otherwise you have to > specify the table names in your model class definitions. > > If that doesn''t help, could you post your class and table definitions > here, so it will be a bit easier to spot what''s wrong. > > //jarkko > > > On 23.2.2005, at 10:57, Joe Van Dyk wrote: > > > A House has_and_belongs_to_many Amenities. > > > > I have the following tables: House, Amenities_Houses, and Amenity. > > house_id and amenity_id are the only things in the join table. > > > > Why on earth does #House.amenities not work? It''s saying that the > > method is not defined. > > > > Using latest stable gems and rails 0.9.5. > > > > Ideas? > > > > Thanks, > > Joe > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > -- > Jarkko Laine > http://jlaine.net > http://odesign.fi > > >
And here''s the particular error I''m getting in the edit method in the Houses controller: Showing /houses/edit.rhtml where line #30 raised undefined method `amenities'' for #<Houses:0x405afce4> 27: 28: <p> 29: <% for amenity in @amenities %> 30: <% if @houses.amenities.include? amenity %> 31: <input type="checkbox" name="<%= amenity.name %>" value="0" checked="checked"/> 32: <% else %> 33: <input type="checkbox" name="<%= amenity.name %>" value="0" /> On Wed, 23 Feb 2005 01:09:07 -0800, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> The table names are all lowercase. > > class House < ActiveRecord::Base > belongs_to :community > belongs_to :image > has_and_belongs_to_many :amenities > has_many :images > end > > class Amenity < ActiveRecord::Base > end > > CREATE TABLE `amenities` ( > `id` int(11) NOT NULL auto_increment, > `title` varchar(255) NOT NULL default '''', > PRIMARY KEY (`id`) > ) TYPE=MyISAM AUTO_INCREMENT=4 ; > > CREATE TABLE `amenities_houses` ( > `house_id` int(11) NOT NULL default ''0'', > `amenity_id` int(11) NOT NULL default ''0'', > PRIMARY KEY (`house_id`,`amenity_id`) > ) TYPE=MyISAM; > > CREATE TABLE `houses` ( > `id` int(11) NOT NULL auto_increment, > `title` varchar(255) NOT NULL default '''', > `price` decimal(10,2) NOT NULL default ''0.00'', > `description` longtext NOT NULL, > `bedrooms` varchar(5) NOT NULL default ''1.0'', > `bathrooms` varchar(5) NOT NULL default '''', > `square_footage` varchar(10) NOT NULL default '''', > `year_built` varchar(4) NOT NULL default ''0'', > `listing_id` varchar(15) NOT NULL default '''', > `street_address` varchar(255) NOT NULL default '''', > `floors` varchar(11) NOT NULL default ''0'', > `image_id` int(11) NOT NULL default ''0'', > `community_id` int(11) NOT NULL default ''0'', > PRIMARY KEY (`id`) > ) TYPE=MyISAM AUTO_INCREMENT=25 ; > > > On Wed, 23 Feb 2005 11:04:30 +0200, Jarkko Laine <jarkko-k1O+Gnc6WpmsTnJN9+BGXg@public.gmane.org> wrote: > > Your table names should be in lower case: houses, amenities and > > amenities_houses. That is what Rails expects. Otherwise you have to > > specify the table names in your model class definitions. > > > > If that doesn''t help, could you post your class and table definitions > > here, so it will be a bit easier to spot what''s wrong. > > > > //jarkko > > > > > > On 23.2.2005, at 10:57, Joe Van Dyk wrote: > > > > > A House has_and_belongs_to_many Amenities. > > > > > > I have the following tables: House, Amenities_Houses, and Amenity. > > > house_id and amenity_id are the only things in the join table. > > > > > > Why on earth does #House.amenities not work? It''s saying that the > > > method is not defined. > > > > > > Using latest stable gems and rails 0.9.5. > > > > > > Ideas? > > > > > > Thanks, > > > Joe > > > _______________________________________________ > > > Rails mailing list > > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > -- > > Jarkko Laine > > http://jlaine.net > > http://odesign.fi > > > > > > >
Shouldn''t the amenity class be: class Amenity < ActiveRecord::Base has_and_belongs_to_many :houses end On Wed, 2005-02-23 at 01:24 -0800, Joe Van Dyk wrote:> > class Amenity < ActiveRecord::Base > > end
Van Aken Martin (GFDI)
2005-Feb-23 09:50 UTC
RE: weird problem with has_and_belongs_to_many
Is it not needed to declare the fields of the join table as foreign keys to Houses & Amenities ? Something like : CREATE TABLE `amenities_houses` ( `house_id` int(11) NOT NULL default ''0'', `amenity_id` int(11) NOT NULL default ''0'',>>> FOREIGN KEY amenity_id REFERENCES amenities(id), >>> FOREIGN KEY house_id REFERENCES houses(id),PRIMARY KEY (`house_id`,`amenity_id`) ) TYPE=MyISAM; Just a guess... Martin -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Joe Van Dyk Sent: Wednesday, February 23, 2005 10:09 AM To: Jarkko Laine; rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: Re: [Rails] weird problem with has_and_belongs_to_many The table names are all lowercase. class House < ActiveRecord::Base belongs_to :community belongs_to :image has_and_belongs_to_many :amenities has_many :images end class Amenity < ActiveRecord::Base end CREATE TABLE `amenities` ( `id` int(11) NOT NULL auto_increment, `title` varchar(255) NOT NULL default '''', PRIMARY KEY (`id`) ) TYPE=MyISAM AUTO_INCREMENT=4 ; CREATE TABLE `amenities_houses` ( `house_id` int(11) NOT NULL default ''0'', `amenity_id` int(11) NOT NULL default ''0'', PRIMARY KEY (`house_id`,`amenity_id`) ) TYPE=MyISAM; CREATE TABLE `houses` ( `id` int(11) NOT NULL auto_increment, `title` varchar(255) NOT NULL default '''', `price` decimal(10,2) NOT NULL default ''0.00'', `description` longtext NOT NULL, `bedrooms` varchar(5) NOT NULL default ''1.0'', `bathrooms` varchar(5) NOT NULL default '''', `square_footage` varchar(10) NOT NULL default '''', `year_built` varchar(4) NOT NULL default ''0'', `listing_id` varchar(15) NOT NULL default '''', `street_address` varchar(255) NOT NULL default '''', `floors` varchar(11) NOT NULL default ''0'', `image_id` int(11) NOT NULL default ''0'', `community_id` int(11) NOT NULL default ''0'', PRIMARY KEY (`id`) ) TYPE=MyISAM AUTO_INCREMENT=25 ; On Wed, 23 Feb 2005 11:04:30 +0200, Jarkko Laine <jarkko-k1O+Gnc6WpmsTnJN9+BGXg@public.gmane.org> wrote:> Your table names should be in lower case: houses, amenities and > amenities_houses. That is what Rails expects. Otherwise you have to > specify the table names in your model class definitions. > > If that doesn''t help, could you post your class and table definitions > here, so it will be a bit easier to spot what''s wrong. > > //jarkko > > > On 23.2.2005, at 10:57, Joe Van Dyk wrote: > > > A House has_and_belongs_to_many Amenities. > > > > I have the following tables: House, Amenities_Houses, and Amenity. > > house_id and amenity_id are the only things in the join table. > > > > Why on earth does #House.amenities not work? It''s saying that the > > method is not defined. > > > > Using latest stable gems and rails 0.9.5. > > > > Ideas? > > > > Thanks, > > Joe > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > -- > Jarkko Laine > http://jlaine.net > http://odesign.fi > > >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Can you should the code that generates @houses? On 23/02/2005, at 8:24 PM, Joe Van Dyk wrote:> And here''s the particular error I''m getting in the edit method in the > Houses controller: > > Showing /houses/edit.rhtml where line #30 raised undefined method > `amenities'' for #<Houses:0x405afce4> > > 27: > 28: <p> > 29: <% for amenity in @amenities %> > 30: <% if @houses.amenities.include? amenity %> > 31: <input type="checkbox" name="<%= amenity.name %>" > value="0" checked="checked"/> > 32: <% else %> > 33: <input type="checkbox" name="<%= amenity.name %>" > value="0" /> > > > > On Wed, 23 Feb 2005 01:09:07 -0800, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: >> The table names are all lowercase. >> >> class House < ActiveRecord::Base >> belongs_to :community >> belongs_to :image >> has_and_belongs_to_many :amenities >> has_many :images >> end >> >> class Amenity < ActiveRecord::Base >> end >> >> CREATE TABLE `amenities` ( >> `id` int(11) NOT NULL auto_increment, >> `title` varchar(255) NOT NULL default '''', >> PRIMARY KEY (`id`) >> ) TYPE=MyISAM AUTO_INCREMENT=4 ; >> >> CREATE TABLE `amenities_houses` ( >> `house_id` int(11) NOT NULL default ''0'', >> `amenity_id` int(11) NOT NULL default ''0'', >> PRIMARY KEY (`house_id`,`amenity_id`) >> ) TYPE=MyISAM; >> >> CREATE TABLE `houses` ( >> `id` int(11) NOT NULL auto_increment, >> `title` varchar(255) NOT NULL default '''', >> `price` decimal(10,2) NOT NULL default ''0.00'', >> `description` longtext NOT NULL, >> `bedrooms` varchar(5) NOT NULL default ''1.0'', >> `bathrooms` varchar(5) NOT NULL default '''', >> `square_footage` varchar(10) NOT NULL default '''', >> `year_built` varchar(4) NOT NULL default ''0'', >> `listing_id` varchar(15) NOT NULL default '''', >> `street_address` varchar(255) NOT NULL default '''', >> `floors` varchar(11) NOT NULL default ''0'', >> `image_id` int(11) NOT NULL default ''0'', >> `community_id` int(11) NOT NULL default ''0'', >> PRIMARY KEY (`id`) >> ) TYPE=MyISAM AUTO_INCREMENT=25 ; >> >> >> On Wed, 23 Feb 2005 11:04:30 +0200, Jarkko Laine <jarkko-k1O+Gnc6WpmsTnJN9+BGXg@public.gmane.org> >> wrote: >>> Your table names should be in lower case: houses, amenities and >>> amenities_houses. That is what Rails expects. Otherwise you have to >>> specify the table names in your model class definitions. >>> >>> If that doesn''t help, could you post your class and table definitions >>> here, so it will be a bit easier to spot what''s wrong. >>> >>> //jarkko >>> >>> >>> On 23.2.2005, at 10:57, Joe Van Dyk wrote: >>> >>>> A House has_and_belongs_to_many Amenities. >>>> >>>> I have the following tables: House, Amenities_Houses, and Amenity. >>>> house_id and amenity_id are the only things in the join table. >>>> >>>> Why on earth does #House.amenities not work? It''s saying that the >>>> method is not defined. >>>> >>>> Using latest stable gems and rails 0.9.5. >>>> >>>> Ideas? >>>> >>>> Thanks, >>>> Joe >>>> _______________________________________________ >>>> Rails mailing list >>>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>>> http://lists.rubyonrails.org/mailman/listinfo/rails >>>> >>> -- >>> Jarkko Laine >>> http://jlaine.net >>> http://odesign.fi >>> >>> >>> >> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On 24/02/2005, at 8:46 AM, Pete Yandell wrote:> Can you should the code that generates @houses?I clearly haven''t woken up yet this morning. That was supposed to read: Can you show the code that generates @houses?> > On 23/02/2005, at 8:24 PM, Joe Van Dyk wrote: > >> And here''s the particular error I''m getting in the edit method in the >> Houses controller: >> >> Showing /houses/edit.rhtml where line #30 raised undefined method >> `amenities'' for #<Houses:0x405afce4> >> >> 27: >> 28: <p> >> 29: <% for amenity in @amenities %> >> 30: <% if @houses.amenities.include? amenity %> >> 31: <input type="checkbox" name="<%= amenity.name %>" >> value="0" checked="checked"/> >> 32: <% else %> >> 33: <input type="checkbox" name="<%= amenity.name %>" >> value="0" /> >> >> >> >> On Wed, 23 Feb 2005 01:09:07 -0800, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >> wrote: >>> The table names are all lowercase. >>> >>> class House < ActiveRecord::Base >>> belongs_to :community >>> belongs_to :image >>> has_and_belongs_to_many :amenities >>> has_many :images >>> end >>> >>> class Amenity < ActiveRecord::Base >>> end >>> >>> CREATE TABLE `amenities` ( >>> `id` int(11) NOT NULL auto_increment, >>> `title` varchar(255) NOT NULL default '''', >>> PRIMARY KEY (`id`) >>> ) TYPE=MyISAM AUTO_INCREMENT=4 ; >>> >>> CREATE TABLE `amenities_houses` ( >>> `house_id` int(11) NOT NULL default ''0'', >>> `amenity_id` int(11) NOT NULL default ''0'', >>> PRIMARY KEY (`house_id`,`amenity_id`) >>> ) TYPE=MyISAM; >>> >>> CREATE TABLE `houses` ( >>> `id` int(11) NOT NULL auto_increment, >>> `title` varchar(255) NOT NULL default '''', >>> `price` decimal(10,2) NOT NULL default ''0.00'', >>> `description` longtext NOT NULL, >>> `bedrooms` varchar(5) NOT NULL default ''1.0'', >>> `bathrooms` varchar(5) NOT NULL default '''', >>> `square_footage` varchar(10) NOT NULL default '''', >>> `year_built` varchar(4) NOT NULL default ''0'', >>> `listing_id` varchar(15) NOT NULL default '''', >>> `street_address` varchar(255) NOT NULL default '''', >>> `floors` varchar(11) NOT NULL default ''0'', >>> `image_id` int(11) NOT NULL default ''0'', >>> `community_id` int(11) NOT NULL default ''0'', >>> PRIMARY KEY (`id`) >>> ) TYPE=MyISAM AUTO_INCREMENT=25 ; >>> >>> >>> On Wed, 23 Feb 2005 11:04:30 +0200, Jarkko Laine <jarkko-k1O+Gnc6WpmsTnJN9+BGXg@public.gmane.org> >>> wrote: >>>> Your table names should be in lower case: houses, amenities and >>>> amenities_houses. That is what Rails expects. Otherwise you have to >>>> specify the table names in your model class definitions. >>>> >>>> If that doesn''t help, could you post your class and table >>>> definitions >>>> here, so it will be a bit easier to spot what''s wrong. >>>> >>>> //jarkko >>>> >>>> >>>> On 23.2.2005, at 10:57, Joe Van Dyk wrote: >>>> >>>>> A House has_and_belongs_to_many Amenities. >>>>> >>>>> I have the following tables: House, Amenities_Houses, and Amenity. >>>>> house_id and amenity_id are the only things in the join table. >>>>> >>>>> Why on earth does #House.amenities not work? It''s saying that the >>>>> method is not defined. >>>>> >>>>> Using latest stable gems and rails 0.9.5. >>>>> >>>>> Ideas? >>>>> >>>>> Thanks, >>>>> Joe >>>>> _______________________________________________ >>>>> Rails mailing list >>>>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>>>> http://lists.rubyonrails.org/mailman/listinfo/rails >>>>> >>>> -- >>>> Jarkko Laine >>>> http://jlaine.net >>>> http://odesign.fi >>>> >>>> >>>> >>> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On 23.2.2005, at 11:50, Van Aken Martin (GFDI) wrote:> Is it not needed to declare the fields of the join table as foreign > keys > to Houses & Amenities ?MyISAM tables don''t support foreign keys, MySQL folks haven''t been believers of referential integrity until recently. On 23.2.2005, at 11:40, Meng Kuan wrote:> Shouldn''t the amenity class be: > > class Amenity < ActiveRecord::Base > has_and_belongs_to_many :houses > endYes, if you need to reach houses from the Amenities'' direction. Otherwise it doesn''t matter. Anyway, I think this is a solved problem: On 23.2.2005, at 11:32, Joe Van Dyk wrote:> On Wed, 23 Feb 2005 11:29:52 +0200, Jarkko Laine <jarkko-k1O+Gnc6WpmsTnJN9+BGXg@public.gmane.org> > wrote: >> >> On 23.2.2005, at 11:24, Joe Van Dyk wrote: >> >>> And here''s the particular error I''m getting in the edit method in the >>> Houses controller: >>> >>> Showing /houses/edit.rhtml where line #30 raised undefined method >>> `amenities'' for #<Houses:0x405afce4> >> >> This should be a House object, not Houses. >> >>> >>> 27: >>> 28: <p> >>> 29: <% for amenity in @amenities %> >>> 30: <% if @houses.amenities.include? amenity %> >> >> Shouldn''t this be @house.amenities. Amenities belong to a single house >> object, right? Unless you call a single house object @houses, of >> course, but <Houses:0x405afce4> indicates something else. >> > > You''re right, I had just figured that out. I was using the > scaffolding that was generated, and it was using Houses.//jarkko -- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails