Hi, I have the following structure class Block < ActiveRecord::Base has_and_belongs_to_many :specs end class Spec < ActiveRecord::Base has_and_belongs_to_many :blocks end There is a table called blocks_specs with three column, block_id, spec_id, value I want to run the following find command to find all blocks, with a spec_id of 1 sorted by blocks_specs.value, i.e. @blocks = Block.find_by_sql("SELECT t.*, j.* FROM blocks_specs j, blocks t WHERE t.id = j.block_id AND j.spec_id = 1 ORDER BY j.value") I have tried to do this with @blocks = Spec.find(1, :order=>"blocks_specs.value").blocks but this doesn''t work. Is this a bug? Thanks, Michael
Hi MIchael. On 9.5.2005, at 11:29, Michael Thompson wrote:> Hi, > I have the following structure > > class Block < ActiveRecord::Base > has_and_belongs_to_many :specs > end > > class Spec < ActiveRecord::Base > has_and_belongs_to_many :blocks > end > > There is a table called blocks_specs with three column, block_id, > spec_id, value > > I want to run the following find command to find all blocks, with a > spec_id of 1 sorted by blocks_specs.value, i.e. > > @blocks = Block.find_by_sql("SELECT t.*, j.* FROM blocks_specs j, > blocks t WHERE t.id = j.block_id AND j.spec_id = 1 ORDER BY j.value") > > I have tried to do this with > > @blocks = Spec.find(1, :order=>"blocks_specs.value").blocks > > but this doesn''t work. Is this a bug?No. Spec.find doesn''t in this case know anything about block_specs so you can''t order by a field in it. You need to give the order parameter to the method fetching blocks, like this: @blocks = Spec.find(1).blocks.find(:all, :order => "blocks_specs.value") //jarkko> > Thanks, Michael > _______________________________________________ > 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 5/9/05, Jarkko Laine <jarkko-k1O+Gnc6WpmsTnJN9+BGXg@public.gmane.org> wrote:> Hi MIchael. > > On 9.5.2005, at 11:29, Michael Thompson wrote: > > > Hi, > > I have the following structure > > > > class Block < ActiveRecord::Base > > has_and_belongs_to_many :specs > > end > > > > class Spec < ActiveRecord::Base > > has_and_belongs_to_many :blocks > > end > > > > There is a table called blocks_specs with three column, block_id, > > spec_id, value > > > > I want to run the following find command to find all blocks, with a > > spec_id of 1 sorted by blocks_specs.value, i.e. > > > > @blocks = Block.find_by_sql("SELECT t.*, j.* FROM blocks_specs j, > > blocks t WHERE t.id = j.block_id AND j.spec_id = 1 ORDER BY j.value") > > > > I have tried to do this with > > > > @blocks = Spec.find(1, :order=>"blocks_specs.value").blocks > > > > but this doesn''t work. Is this a bug? > > No. Spec.find doesn''t in this case know anything about block_specs so > you can''t order by a field in it. You need to give the order parameter > to the method fetching blocks, like this: > > @blocks = Spec.find(1).blocks.find(:all, :order => "blocks_specs.value") >I tried that and I get Couldn''t find Block with ID in (''--- :all'',''--- \n:order: blocks_specs.value'') I''m running ruby 1.8 and activerecord-1.9.1 if that has any affect. Thanks, Michael
On 9.5.2005, at 13:23, Michael Thompson wrote:> Actually spoke to soon, using the following command > > @blocks = Spec.find(1).blocks.find(:all) > > I get this in my browser > > Couldn''t find Block with ID in (''--- :all'') > > and the following in the log file, any ideas? > > SELECT t.*, j.* FROM blocks_specs j, blocks t WHERE t.id = j.block_id > AND j.spec_id = 1 AND j.block_id IN (''--- :all'') > > > ActiveRecord::RecordNotFound (Couldn''t find Block with ID in (''--- > :all'')): > > /usr/lib/ruby/gems/1.8/gems/activerecord-1.10.1/lib/active_record/ > associations/has_and_belongs_to_many_association.rb:77:in > `find''Hmmm... where did it work, then? Maybe you need to restart your webserver so that it will grab the katest code, too. //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
Michael, On 9.5.2005, at 15:15, Michael Thompson wrote:>> Hmmm... where did it work, then? Maybe you need to restart your >> webserver so that it will grab the katest code, too. > > Tried re-starting the web server didn''t help. > > I thought it was working before but I hadn''t saved the changes to a > file (they hadn''t been copied to the remote server yet by xemacs). > > So the following two statements work > > @blocks = Spec.find(1).blocks > @blocks = Spec.find(1).blocks.find([28, 20]) > > and produce sql like > > SELECT t.*, j.* FROM blocks_specs j, blocks t WHERE t.id = j.block_id > AND j.spec_id = 1 AND j.block_id IN (28,20) > > but this fails > > @blocks = Spec.find(1).blocks.find(:all) > > producing this error Couldn''t find Block with ID in (''--- :all'')This really seems like some old code running. Can you try script/console and try these commands by hand there? //jarkko P.S. Could you please send your responses to the list, not to me directly? You''ll get a wider audience ;-) -- 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 5/9/05, Michael Thompson <michaelnt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 5/9/05, Jarkko Laine <jarkko-k1O+Gnc6WpmsTnJN9+BGXg@public.gmane.org> wrote: > > Hi MIchael. > > > > On 9.5.2005, at 11:29, Michael Thompson wrote: > > > > > Hi, > > > I have the following structure > > > > > > class Block < ActiveRecord::Base > > > has_and_belongs_to_many :specs > > > end > > > > > > class Spec < ActiveRecord::Base > > > has_and_belongs_to_many :blocks > > > end > > > > > > There is a table called blocks_specs with three column, block_id, > > > spec_id, value > > > > > > I want to run the following find command to find all blocks, with a > > > spec_id of 1 sorted by blocks_specs.value, i.e. > > > > > > @blocks = Block.find_by_sql("SELECT t.*, j.* FROM blocks_specs j, > > > blocks t WHERE t.id = j.block_id AND j.spec_id = 1 ORDER BY j.value") > > > > > > I have tried to do this with > > > > > > @blocks = Spec.find(1, :order=>"blocks_specs.value").blocks > > > > > > but this doesn''t work. Is this a bug? > > > > No. Spec.find doesn''t in this case know anything about block_specs so > > you can''t order by a field in it. You need to give the order parameter > > to the method fetching blocks, like this: > > > > @blocks = Spec.find(1).blocks.find(:all, :order => "blocks_specs.value") > >I have now update fails and still have this problem, using ActiveRecord version 1.10.1 Running the console the folloowing command works Spec.find(1).blocks.find(28) but this command fails irb(main):012:0> Spec.find(1).blocks.find(:all) ActiveRecord::RecordNotFound: Couldn''t find Block with ID in (''--- :all'') from /usr/lib/ruby/gems/1.8/gems/activerecord-1.10.1/lib/active_record/associations/has_and_belongs_to_many_association.rb:77:in `find'' from (irb):12 Any ideas?
On 09/05/2005, at 10:23 PM, Jarkko Laine wrote:> On 9.5.2005, at 15:15, Michael Thompson wrote: > >> So the following two statements work >> >> @blocks = Spec.find(1).blocks >> @blocks = Spec.find(1).blocks.find([28, 20]) >> >> and produce sql like >> >> SELECT t.*, j.* FROM blocks_specs j, blocks t WHERE t.id = j.block_id >> AND j.spec_id = 1 AND j.block_id IN (28,20) >> >> but this fails >> >> @blocks = Spec.find(1).blocks.find(:all) >> >> producing this error Couldn''t find Block with ID in (''--- :all'') >> > > This really seems like some old code running. Can you try script/ > console and try these commands by hand there?I can confirm the same behaviour on a fresh install of rail 0.12.1. Here''s the irb output: irb(main):003:0> folder = Folder.find :first => #<Folder:0x254aa00 @attributes={"name"=>"Gizmodo", "id"=>"16"}> irb(main):004:0> folder.messages.find :all ActiveRecord::RecordNotFound: Couldn''t find Message with ID in (''--- :all'') from /opt/local/lib/ruby/gems/1.8/gems/activerecord-1.10.1/ lib/active_record/associations/has_and_belongs_to_many_association.rb: 77:in `find'' from (irb):4 Looking at the code for find in has_and_belongs_to_many_association this is exactly the error I''d expect to get. The find code flattens its arguments into a list of ids to search for and doesn''t handle any of the usual find parameters. (I was, in fact, having this exact same problem earlier in the day while trying to solve the habtm and pagination problems I posted about earlier.) Cheers, Pete Yandell
On 9.5.2005, at 15:36, Michael Thompson wrote:> I have now update fails and still have this problem, using > ActiveRecord version 1.10.1 > > Running the console the folloowing command works > > Spec.find(1).blocks.find(28) > > but this command fails > > irb(main):012:0> Spec.find(1).blocks.find(:all) > ActiveRecord::RecordNotFound: Couldn''t find Block with ID in (''--- > :all'') > from > /usr/lib/ruby/gems/1.8/gems/activerecord-1.10.1/lib/active_record/ > associations/has_and_belongs_to_many_association.rb:77:in > `find'' > from (irb):12 > > Any ideas?Hmmm... it actually seems that has_and_belongs_to_many doesn''t support the new syntax for find (see http://rails.rubyonrails.com/classes/ActiveRecord/Associations/ ClassMethods.html#M000436) like has_many does. So it seems that at the moment find(id) is all that works for them. However, remember that you''ll get exactly the same with Spec.find(1).blocks as with Spec.find(1).blocks.find(:all). Of course it doesn''t help you if need one of the more advanced parameters. At the moment it might be easiest to wrap your find_by_sql call inside a method in Block class: def find_all_by_spec(spec_id) Block.find_by_sql(["SELECT t.*, j.* FROM blocks_specs j, blocks t WHERE t.id = j.block_id AND j.spec_id = ? ORDER BY j.value", spec_id]) end //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
On 5/9/05, Jarkko Laine <jarkko-k1O+Gnc6WpmsTnJN9+BGXg@public.gmane.org> wrote:> Hmmm... it actually seems that has_and_belongs_to_many doesn''t support > the new syntax for find (see > http://rails.rubyonrails.com/classes/ActiveRecord/Associations/ > ClassMethods.html#M000436) like has_many does. So it seems that at the > moment find(id) is all that works for them.Should I file this as a bug or enhancement request? Michael
On 9.5.2005, at 16:23, Michael Thompson wrote:> On 5/9/05, Jarkko Laine <jarkko-k1O+Gnc6WpmsTnJN9+BGXg@public.gmane.org> wrote: >> Hmmm... it actually seems that has_and_belongs_to_many doesn''t support >> the new syntax for find (see >> http://rails.rubyonrails.com/classes/ActiveRecord/Associations/ >> ClassMethods.html#M000436) like has_many does. So it seems that at the >> moment find(id) is all that works for them. > > Should I file this as a bug or enhancement request?Sure, http://dev.rubyonrails.com. Enhancement request is probably better, since this is actually documented behavior for habtm relationships. //jarkko> > Michael > _______________________________________________ > 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