using rails 0.13.1 class Order < ActiveRecord::Base # retrieve all orders whose line items reference boxes located in a given facility def self.find_by_facility(facility) find(:select => "distinct o.*", :joins => "as o, line_items as li, boxes as b, zones as z", :conditions => ["li.order_id = o.id <http://o.id> and b.id <http://b.id> li.box_id and z.id <http://z.id> = b.zone_id and z.facility = ?", facility], :order => "o.order_num") end end in controller @orders = Order.find_by_facility(@params[:facility_id]) this results in the following error: ActiveRecord::RecordNotFound (Couldn''t find Order without an ID AND li.order_id = o.id <http://o.id> and b.id <http://b.id> = li.box_id and z.id<http://z.id>b.zone_id and z.facility_id = 1): from the docs: :select: By default, this is * as in SELECT * FROM, but can be changed if you for example want to do a join, but not include the joined columns. the way i read this is that :select is what goes between "SELECT" and "FROM" but find() is expecting either an ID, :all or :first. i''ve tried adding :all, but that results in :select not being used and i get duplicate rows returned. if i rewrite the method using find_by_sql, it works as expected... so what''s up with :select then? Chris _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Hi ! 2005/11/4, Chris Hall <christopher.k.hall@gmail.com>:> using rails 0.13.1That's your problem right there. :select is a new option in Rails 0.14. Stick with your find_by_sql method, or upgrade to Rails 0.14.2. Bye ! François _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Nov 4, 2005, at 11:38 AM, Chris Hall wrote:> using rails 0.13.1:select is introduced in 0.14 (1.0rc1)> class Order < ActiveRecord::Base > # retrieve all orders whose line items reference boxes located in > a given facility > def self.find_by_facility(facility) > find(:select => "distinct o.*",find(:all, :select => ''distinct o.*'',> :joins => "as o, line_items as li, boxes as b, zones as > z", > :conditions => ["li.order_id = o.id and b.id = > li.box_id and z.id = b.zone_id and z.facility = ?", facility], > :order => "o.order_num") > end > endNice query :-) jeremy -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (Darwin) iD8DBQFDa7r7AQHALep9HFYRAjLhAJ48zB+eRnRRDVYLzrKBI6sXWlXwnwCglamc 1MOaUiGVzwcR+t7RRzA7UKI=4jQ/ -----END PGP SIGNATURE-----
ah, that explains it... thanks for the info. On 11/4/05, Jeremy Kemper <jeremy-w7CzD/W5Ocjk1uMJSBkQmQ@public.gmane.org> wrote:> > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > On Nov 4, 2005, at 11:38 AM, Chris Hall wrote: > > using rails 0.13.1 > > :select is introduced in 0.14 (1.0rc1) > > > > class Order < ActiveRecord::Base > > # retrieve all orders whose line items reference boxes located in > > a given facility > > def self.find_by_facility(facility) > > find(:select => "distinct o.*", > > find(:all, :select => ''distinct o.*'', > > > > :joins => "as o, line_items as li, boxes as b, zones as > > z", > > :conditions => ["li.order_id = o.id <http://o.id> and b.id <http://b.id>> > li.box_id and z.id <http://z.id> = b.zone_id and z.facility = ?", > facility], > > :order => "o.order_num") > > end > > end > > > Nice query :-) > > jeremy > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.4.2 (Darwin) > > iD8DBQFDa7r7AQHALep9HFYRAjLhAJ48zB+eRnRRDVYLzrKBI6sXWlXwnwCglamc > 1MOaUiGVzwcR+t7RRzA7UKI> =4jQ/ > -----END PGP SIGNATURE----- > _______________________________________________ > 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