I am trying to use the association functions within rails to query some tables. I am trying to query the address table for all addresses associated with a single person. In the PersonController I''m running this @person#addresses.find I would expect this to query the database and join the tables together and return all the address associated with @person. However, when I look at the mysql general query log, I don''t see any queries being generated. I''ve listed the relevant info at the bottom of the e-mail. Any help on what I''m doing wrong would be appreciated. Thanks Amith =========================================================================person.rb =========================================================================class Person < ActiveRecord::Base has_and_belongs_to_many :addresses, :join_table => "people_addresses" has_and_belongs_to_many :emails, :join_table => "people_emails" has_and_belongs_to_many :phone_numbers, :join_table => "people_phone_numbers" has_and_belongs_to_many :people, :join_table => "relationships" end =========================================================================address.rb =========================================================================class Address < ActiveRecord::Base has_and_belongs_to_many :people, :join_table => "person_addresses" belongs_to :location belongs_to :state end =========================================================================person_controller.rb ========================================================================= def edit @person = Person.find(@params["id"]) @address = @person#addresses.find end mysql> desc people; +--------------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------------+------------------+------+-----+---------+----------------+ | id | int(10) unsigned | | PRI | NULL | auto_increment | | first_name | varchar(50) | YES | | NULL | | | last_name | varchar(50) | YES | | NULL | | +--------------+------------------+------+-----+---------+----------------+ desc addresses; +-----------------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------------+------------------+------+-----+---------+----------------+ | id | int(10) unsigned | | PRI | NULL | auto_increment | | street_address | varchar(100) | | | | | | apt_no | varchar(20) | YES | | NULL | | | city | varchar(50) | YES | | NULL | | | state_id | int(10) unsigned | YES | MUL | NULL | | | zip_cd | varchar(10) | YES | | NULL | | | country | varchar(50) | YES | | NULL | | | location_id | int(10) unsigned | YES | MUL | NULL | | | last_updated | timestamp(14) | YES | | NULL | | | street_address2 | varchar(100) | YES | | NULL | | +-----------------+------------------+------+-----+---------+----------------+ mysql> desc people_addresses; +------------+------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------+------------------+------+-----+---------+-------+ | person_id | int(10) unsigned | | PRI | 0 | | | address_id | int(10) unsigned | | PRI | 0 | | +------------+------------------+------+-----+---------+-------+
@person.addresses.find should accept an argument to search the addresses collection for a particular address object. If you want all of them, call @person.addresses and a collection containing them will be returned. Brian On Tue, 8 Feb 2005 17:08:37 -0500, Amith Varghese <amith-sCgbW8QO9uUAvxtiuMwx3w@public.gmane.org> wrote:> I am trying to use the association functions within rails to query some > tables. I am trying to query the address table for all addresses associated > with a single person. In the PersonController I''m running this > > @person#addresses.find > > I would expect this to query the database and join the tables together and > return all the address associated with @person. However, when I look at the > mysql general query log, I don''t see any queries being generated. I''ve listed > the relevant info at the bottom of the e-mail. Any help on what I''m doing > wrong would be appreciated. > > Thanks > Amith > > =========================================================================> person.rb > =========================================================================> class Person < ActiveRecord::Base > has_and_belongs_to_many :addresses, :join_table => "people_addresses" > has_and_belongs_to_many :emails, :join_table => "people_emails" > has_and_belongs_to_many :phone_numbers, :join_table > => "people_phone_numbers" > has_and_belongs_to_many :people, :join_table => "relationships" > end > > =========================================================================> address.rb > =========================================================================> class Address < ActiveRecord::Base > has_and_belongs_to_many :people, :join_table => "person_addresses" > belongs_to :location > belongs_to :state > end > > =========================================================================> person_controller.rb > =========================================================================> def edit > @person = Person.find(@params["id"]) > > @address = @person#addresses.find > end > > mysql> desc people; > +--------------+------------------+------+-----+---------+----------------+ > | Field | Type | Null | Key | Default | Extra | > +--------------+------------------+------+-----+---------+----------------+ > | id | int(10) unsigned | | PRI | NULL | auto_increment | > | first_name | varchar(50) | YES | | NULL | | > | last_name | varchar(50) | YES | | NULL | | > +--------------+------------------+------+-----+---------+----------------+ > > desc addresses; > +-----------------+------------------+------+-----+---------+----------------+ > | Field | Type | Null | Key | Default | Extra | > +-----------------+------------------+------+-----+---------+----------------+ > | id | int(10) unsigned | | PRI | NULL | auto_increment | > | street_address | varchar(100) | | | | | > | apt_no | varchar(20) | YES | | NULL | | > | city | varchar(50) | YES | | NULL | | > | state_id | int(10) unsigned | YES | MUL | NULL | | > | zip_cd | varchar(10) | YES | | NULL | | > | country | varchar(50) | YES | | NULL | | > | location_id | int(10) unsigned | YES | MUL | NULL | | > | last_updated | timestamp(14) | YES | | NULL | | > | street_address2 | varchar(100) | YES | | NULL | | > +-----------------+------------------+------+-----+---------+----------------+ > > mysql> desc people_addresses; > +------------+------------------+------+-----+---------+-------+ > | Field | Type | Null | Key | Default | Extra | > +------------+------------------+------+-----+---------+-------+ > | person_id | int(10) unsigned | | PRI | 0 | | > | address_id | int(10) unsigned | | PRI | 0 | | > +------------+------------------+------+-----+---------+-------+ > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- The years ahead pick up their dark bags. They move closer. There''s a slight rise in the silence then nothing. -- (If you''re receiving this in response to mail sent to bluczkie-OM76b2Iv3yLQjUSlxSEPGw@public.gmane.org, don''t be concerned This is my new address, but mail will be forwarded here indefinitely)
Quoting Amith Varghese <amith-sCgbW8QO9uUAvxtiuMwx3w@public.gmane.org>:> I am trying to use the association functions within rails to query some > tables. I am trying to query the address table for all addresses associated > with a single person. In the PersonController I''m running this > > @person#addresses.findThis is an unfortunate occasional side effect of one of Ruby''s documentation conventions, that of using the `#` symbol to denote instance methods - a convention that is followed in the Rails documentation. Person#addresses means that instances of Person (eg @person) will have an `addresses` method, but this method is called using the usual `.` notation, not using a `#` (which starts a comment). So the line @person#addresses is equivalent to @person # This is a comment which you might have picked up if you were using a syntax-highlighting editor with Ruby support, but not have much chance of noticing otherwise. What you really want to do, as pointed out in a previous email, is: @person.addresses.find Tim. -- Tim Bates tim-kZbwfhiKUx26c6uEtOJ/EA@public.gmane.org
Quoting Tim Bates <tim-kZbwfhiKUx26c6uEtOJ/EA@public.gmane.org>:> This is an unfortunate occasional side effect of one of Ruby''s documentation > conventions, that of using the `#` symbol to denote instance methods - a > convention that is followed in the Rails documentation. > Person#addresses means > that instances of Person (eg @person) will have an `addresses` > method, but this > method is called using the usual `.` notation, not using a `#` (which > starts a > comment). So the line > > @person#addresses > > is equivalent to > > @person # This is a comment > > which you might have picked up if you were using a syntax-highlighting editor > with Ruby support, but not have much chance of noticing otherwise. What you > really want to do, as pointed out in a previous email, is: > > @person.addresses.find > ><sigh>.... This was my problem. I''m new to Rails and Ruby so I don''t know the syntax real well and was just following the documentation. I think its pretty cool how all this works. Maybe I should spend some more time reading the ruby documentation to get a better understanding :) Amith
Hi Amith and welcome onboard, On 9.2.2005, at 06:36, amith-sCgbW8QO9uUAvxtiuMwx3w@public.gmane.org wrote:> > <sigh>.... This was my problem. I''m new to Rails and Ruby so I don''t > know the > syntax real well and was just following the documentation. I think > its pretty > cool how all this works. Maybe I should spend some more time reading > the ruby > documentation to get a better understanding :)http://www.rubycentral.com/book/ would be a good place to start. If you like it, you might want to consider buying the second edition with much more and fresher content (e.g. testing and gems): http://pragmaticprogrammer.com/titles/ruby/index.html //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