Sorry for asking a basic question, but I can''t seem to get the syntax right for a find query. I have a table ''venues'' that has an id to the addresses table. I want to list all the venues, sorted by city. I actually want to use the paginate() function, but I can''t even get a find() to work. @venues = Venue.find :all, :conditions => "address_id = addresses.id", :order_by => ''addresses.city'' gives me Unknown table ''addresses'' in where clause: SELECT * FROM venues WHERE address_id = addresses.id Figure i need to add an include to get the table name in there, as per the documentation: @venues = Venue.find :all, :conditions => "address_id = addresses.id", :include => [ :addresses ], :order_by => ''addresses.city'' gives me undefined method `table_name'' for nil:NilClass Trying with singular version of addresses: @venues = Venue.find :all, :conditions => "address_id = addresses.id", :include => [ :address ], :order_by => ''addresses.city'' gives me uninitialized constant Addres It''s trying to load Addres.rb Adding :join doesn''t seem to make any difference. What''s wrong with my syntax? Or is there a problem in find? Thanks for any help, Brett _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
After some more research, it looks like other people have run into this - the pluralization is not correct. But i haven''t really seen a solution. I''ve tried putting set_table_name "addresses" in the Address model, and belongs_to :address, :class_name => ''Address'' in the venue model. Nothing helps. Any suggestions? Brett On May 22, 2005, at 1:08 PM, Brett Walker wrote:> Sorry for asking a basic question, but I can''t seem to get the > syntax right for a find query. > > I have a table ''venues'' that has an id to the addresses table. I > want to list all the venues, sorted by city. I actually want to > use the paginate() function, but I can''t even get a find() to work. > > @venues = Venue.find :all, > :conditions => "address_id = addresses.id", > :order_by => ''addresses.city'' > > gives me > > Unknown table ''addresses'' in where clause: SELECT * FROM venues > WHERE address_id = addresses.id > > Figure i need to add an include to get the table name in there, as > per the documentation: > > @venues = Venue.find :all, > :conditions => "address_id = addresses.id", > :include => [ :addresses ], > :order_by => ''addresses.city'' > > gives me > > undefined method `table_name'' for nil:NilClass > > Trying with singular version of addresses: > > @venues = Venue.find :all, > :conditions => "address_id = addresses.id", > :include => [ :address ], > :order_by => ''addresses.city'' > > gives me > > uninitialized constant Addres > > It''s trying to load Addres.rb > > Adding :join doesn''t seem to make any difference. What''s wrong > with my syntax? Or is there a problem in find? > > Thanks for any help, > Brett > _______________________________________________ > 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
When you use the :include directive, it shouldn''t be necessary to explicitly tell ActiveRecord that the address_id = addresses.id in the :conditions. Otherwise, I''m not seeing anything wrong with the syntax. You''re correct in using the plural form of "addresses" in the :include array. Duane Johnson (canadaduane) On May 22, 2005, at 2:34 PM, Brett Walker wrote:> After some more research, it looks like other people have run into > this - the pluralization is not correct. But i haven''t really seen > a solution. > > I''ve tried putting > > set_table_name "addresses" > > in the Address model, and > > belongs_to :address, :class_name => ''Address'' > > in the venue model. Nothing helps. > > Any suggestions? > > Brett > > On May 22, 2005, at 1:08 PM, Brett Walker wrote: > >> Sorry for asking a basic question, but I can''t seem to get the >> syntax right for a find query. >> >> I have a table ''venues'' that has an id to the addresses table. I >> want to list all the venues, sorted by city. I actually want to >> use the paginate() function, but I can''t even get a find() to work. >> >> @venues = Venue.find :all, >> :conditions => "address_id = addresses.id", >> :order_by => ''addresses.city'' >> >> gives me >> >> Unknown table ''addresses'' in where clause: SELECT * FROM venues >> WHERE address_id = addresses.id >> >> Figure i need to add an include to get the table name in there, as >> per the documentation: >> >> @venues = Venue.find :all, >> :conditions => "address_id = addresses.id", >> :include => [ :addresses ], >> :order_by => ''addresses.city'' >> >> gives me >> >> undefined method `table_name'' for nil:NilClass >> >> Trying with singular version of addresses: >> >> @venues = Venue.find :all, >> :conditions => "address_id = addresses.id", >> :include => [ :address ], >> :order_by => ''addresses.city'' >> >> gives me >> >> uninitialized constant Addres >> >> It''s trying to load Addres.rb >> >> Adding :join doesn''t seem to make any difference. What''s wrong >> with my syntax? Or is there a problem in find? >> >> Thanks for any help, >> Brett >> _______________________________________________ >> 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 >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Duane, You''re right, the condition is not needed. But the real problem looks like a bug in determining the table name. If I use belongs_to :address, :class_name => ''Address'' in the Venue object, it works. But ONLY after restarting WebBrick. I had tried this change before, based on a thread I saw, but it continued to fail. For some reason, WebBrick (and the console) caches that particular information, requiring it to be bounced to pick up any changes. There is also problem where doing a Venue.find_all(:include => :address, :order_by => ''city'') gives an error ''StatementInvalid: Unknown column ''includeaddressorder_bycity'' in ''where clause'': SELECT * FROM venues WHERE includeaddressorder_bycity'' - the parameters are being run together. Question to everyone: is everyone using the bleeding edge svn version, or the 0.12.1 code? Should I be getting the trunk every night? Cheers, Brett On May 22, 2005, at 6:25 PM, Duane Johnson wrote:> When you use the :include directive, it shouldn''t be necessary to > explicitly tell ActiveRecord that the address_id = addresses.id in > the :conditions. > > Otherwise, I''m not seeing anything wrong with the syntax. You''re > correct in using the plural form of "addresses" in the :include array. > > Duane Johnson > (canadaduane) > > On May 22, 2005, at 2:34 PM, Brett Walker wrote: > >> After some more research, it looks like other people have run into >> this - the pluralization is not correct. But i haven''t really >> seen a solution. >> >> I''ve tried putting >> >> set_table_name "addresses" >> >> in the Address model, and >> >> belongs_to :address, :class_name => ''Address'' >> >> in the venue model. Nothing helps. >> >> Any suggestions? >> >> Brett >> >> On May 22, 2005, at 1:08 PM, Brett Walker wrote: >> >>> Sorry for asking a basic question, but I can''t seem to get the >>> syntax right for a find query. >>> >>> I have a table ''venues'' that has an id to the addresses table. I >>> want to list all the venues, sorted by city. I actually want to >>> use the paginate() function, but I can''t even get a find() to work. >>> >>> @venues = Venue.find :all, >>> :conditions => "address_id = addresses.id", >>> :order_by => ''addresses.city'' >>> >>> gives me >>> >>> Unknown table ''addresses'' in where clause: SELECT * FROM venues >>> WHERE address_id = addresses.id >>> >>> Figure i need to add an include to get the table name in there, >>> as per the documentation: >>> >>> @venues = Venue.find :all, >>> :conditions => "address_id = addresses.id", >>> :include => [ :addresses ], >>> :order_by => ''addresses.city'' >>> >>> gives me >>> >>> undefined method `table_name'' for nil:NilClass >>> >>> Trying with singular version of addresses: >>> >>> @venues = Venue.find :all, >>> :conditions => "address_id = addresses.id", >>> :include => [ :address ], >>> :order_by => ''addresses.city'' >>> >>> gives me >>> >>> uninitialized constant Addres >>> >>> It''s trying to load Addres.rb >>> >>> Adding :join doesn''t seem to make any difference. What''s wrong >>> with my syntax? Or is there a problem in find? >>> >>> Thanks for any help, >>> Brett >>> _______________________________________________ >>> 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 >> > > _______________________________________________ > 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
Michael Koziarski
2005-May-23 03:22 UTC
Re: Need help with find syntax and associations...
On 5/23/05, Brett Walker <walkerbl-O5WfVfzUwx8@public.gmane.org> wrote:> Duane, > > You''re right, the condition is not needed. But the real problem looks like > a bug in determining the table name. If I use > > belongs_to :address, :class_name => ''Address'' > > in the Venue object, it works. But ONLY after restarting WebBrick. I had > tried this change before, based on a thread I saw, but it continued to fail. > For some reason, WebBrick (and the console) caches that particular > information, requiring it to be bounced to pick up any changes. > > There is also problem where doing a > > Venue.find_all(:include => :address, :order_by => ''city'')This should be: Venue.find(:all, :include => :address, :order_by => ''city'')> gives an error ''StatementInvalid: Unknown column > ''includeaddressorder_bycity'' in ''where clause'': SELECT * FROM venues WHERE > includeaddressorder_bycity'' - the parameters are being run together. > > Question to everyone: is everyone using the bleeding edge svn version, or > the 0.12.1 code? Should I be getting the trunk every night?The releases should be stable, you haven''t needed trunk for quite some time.> Cheers, > Brett > > > On May 22, 2005, at 6:25 PM, Duane Johnson wrote: > When you use the :include directive, it shouldn''t be necessary to > explicitly tell ActiveRecord that the address_id = addresses.id in the > :conditions. > > Otherwise, I''m not seeing anything wrong with the syntax. You''re correct in > using the plural form of "addresses" in the :include array. > > Duane Johnson > (canadaduane) > > > On May 22, 2005, at 2:34 PM, Brett Walker wrote: > After some more research, it looks like other people have run into this - > the pluralization is not correct. But i haven''t really seen a solution. > > I''ve tried putting > > set_table_name "addresses" > > in the Address model, and > > belongs_to :address, :class_name => ''Address'' > > in the venue model. Nothing helps. > > Any suggestions? > > Brett > > > On May 22, 2005, at 1:08 PM, Brett Walker wrote: > Sorry for asking a basic question, but I can''t seem to get the syntax right > for a find query. > > I have a table ''venues'' that has an id to the addresses table. I want to > list all the venues, sorted by city. I actually want to use the paginate() > function, but I can''t even get a find() to work. > > @venues = Venue.find :all, > :conditions => "address_id = addresses.id", > :order_by => ''addresses.city'' > > gives me > > Unknown table ''addresses'' in where clause: SELECT * FROM venues WHERE > address_id = addresses.id > > Figure i need to add an include to get the table name in there, as per the > documentation: > > @venues = Venue.find :all, > :conditions => "address_id = addresses.id", > :include => [ :addresses ], > :order_by => ''addresses.city'' > > gives me > > undefined method `table_name'' for nil:NilClass > > Trying with singular version of addresses: > > @venues = Venue.find :all, > :conditions => "address_id = addresses.id", > :include => [ :address ], > :order_by => ''addresses.city'' > > gives me > > uninitialized constant Addres > > It''s trying to load Addres.rb > > Adding :join doesn''t seem to make any difference. What''s wrong with my > syntax? Or is there a problem in find? > > Thanks for any help, > Brett > _______________________________________________ > 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 > > _______________________________________________ > 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 > > >-- Cheers Koz