I have the following expression: user.clocks.includes(:runs => :user_runs).includes(:users).find_by_id(params[:id]) which seems to work fine. But when I add an orders, like this: user.clocks.includes(:runs => :user_runs).includes(:users).orders("users.names").find_by_id(params[:id]) it breaks with the following error: ActiveRecord::ConfigurationError: Association named ''user_runs'' was not found; perhaps you misspelled it? app/controllers/clocks_controller.rb:19:in `show'' test/functional/clocks_controller_test.rb:21:in `__bind_1286475263_942556'' Any ideas why? The model looks like this: class Clock < ActiveRecord::Base has_and_belongs_to_many :users has_many :runs end class Run < ActiveRecord::Base belongs_to :clock has_many :user_runs has_many :users, :through => :user_runs end class UserRun < ActiveRecord::Base belongs_to :run belongs_to :user end -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
J. Pablo Fernández
2010-Oct-07 20:27 UTC
Re: Not being able to order by includes on Rails3
Continuing with my investigation I''ve tried this: ubiquitous_user.clocks.includes(:runs => :user_runs).find_by_id(params[:id]) and I''ve noticed the queries it''s generating doesn''t get user_runs at all. On Oct 7, 9:51 pm, J. Pablo Fernández <pup...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have the following expression: > > user.clocks.includes(:runs > => :user_runs).includes(:users).find_by_id(params[:id]) > > which seems to work fine. But when I add an orders, like this: > > user.clocks.includes(:runs > => :user_runs).includes(:users).orders("users.names").find_by_id(params[:id]) > > it breaks with the following error: > > ActiveRecord::ConfigurationError: Association named ''user_runs'' > was not found; perhaps you misspelled it? > app/controllers/clocks_controller.rb:19:in `show'' > test/functional/clocks_controller_test.rb:21:in > `__bind_1286475263_942556'' > > Any ideas why? > > The model looks like this: > > class Clock < ActiveRecord::Base > has_and_belongs_to_many :users > has_many :runs > end > > class Run < ActiveRecord::Base > belongs_to :clock > has_many :user_runs > has_many :users, :through => :user_runs > end > > class UserRun < ActiveRecord::Base > belongs_to :run > belongs_to :user > end-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
J. Pablo Fernández
2010-Oct-08 04:55 UTC
Re: Not being able to order by includes on Rails3
I''ve created a set of tests to try to figure what was going on: context "A graph of users, clocks, runs, etc" do setup do @users = [] 10.times do @users << Factory.create(:user) end @clocks = [] 10.times do @clocks << Factory.create(:clock, :users => @users) end @clocks.each do |clock| 10.times do run = Factory.create :run, :clock => clock @users.each do |user| Factory.create :user_run, :run => run, :user => user end end end @user = @users.first @clock = @clocks.first end should "find a clock" do assert_not_nil @user.clocks.find(@clock.id) end should "find a clock with users" do assert_not_nil @user.clocks.includes(:users).find(@clock.id) end should "find a clock with users and runs" do assert_not_nil @user.clocks.includes(:users, :runs).find(@clock.id) end should "find a clock with users, runs and user_runs" do assert_not_nil @user.clocks.includes(:users, :runs => :user_runs).find(@clock.id) end should "find a clock with users order by users.name" do assert_not_nil @user.clocks.includes(:users).order("users.name").find(@clock.id) end should "find a clock with users and runs order by users.name" do assert_not_nil @user.clocks.includes(:users, :runs).order("users.name").find(@clock.id) end should "find a clock with users, runs and user_runs order by users.name" do assert_not_nil @user.clocks.includes(:users, :runs => :user_runs).order("users.name").find(@clock.id) end end Every test but the last one pass. Is this not a bug? On Oct 7, 10:27 pm, J. Pablo Fernández <pup...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Continuing with my investigation I''ve tried this: > > ubiquitous_user.clocks.includes(:runs > => :user_runs).find_by_id(params[:id]) > > and I''ve noticed the queries it''s generating doesn''t get user_runs at > all. > > On Oct 7, 9:51 pm, J. Pablo Fernández <pup...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > I have the following expression: > > > user.clocks.includes(:runs > > => :user_runs).includes(:users).find_by_id(params[:id]) > > > which seems to work fine. But when I add an orders, like this: > > > user.clocks.includes(:runs > > => :user_runs).includes(:users).orders("users.names").find_by_id(params[:id]) > > > it breaks with the following error: > > > ActiveRecord::ConfigurationError: Association named ''user_runs'' > > was not found; perhaps you misspelled it? > > app/controllers/clocks_controller.rb:19:in `show'' > > test/functional/clocks_controller_test.rb:21:in > > `__bind_1286475263_942556'' > > > Any ideas why? > > > The model looks like this: > > > class Clock < ActiveRecord::Base > > has_and_belongs_to_many :users > > has_many :runs > > end > > > class Run < ActiveRecord::Base > > belongs_to :clock > > has_many :user_runs > > has_many :users, :through => :user_runs > > end > > > class UserRun < ActiveRecord::Base > > belongs_to :run > > belongs_to :user > > end-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
J. Pablo Fernández
2010-Oct-08 05:10 UTC
Re: Not being able to order by includes on Rails3
I am now convinced this is a bug, so I reported it here: https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/5768 On Oct 8, 6:55 am, J. Pablo Fernández <pup...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''ve created a set of tests to try to figure what was going on: > > context "A graph of users, clocks, runs, etc" do > setup do > @users = [] > 10.times do > @users << Factory.create(:user) > end > @clocks = [] > 10.times do > @clocks << Factory.create(:clock, :users => @users) > end > @clocks.each do |clock| > 10.times do > run = Factory.create :run, :clock => clock > @users.each do |user| > Factory.create :user_run, :run => run, :user => user > end > end > end > @user = @users.first > @clock = @clocks.first > end > > should "find a clock" do > assert_not_nil @user.clocks.find(@clock.id) > end > > should "find a clock with users" do > assert_not_nil @user.clocks.includes(:users).find(@clock.id) > end > > should "find a clock with users and runs" do > assert_not_nil > @user.clocks.includes(:users, :runs).find(@clock.id) > end > > should "find a clock with users, runs and user_runs" do > assert_not_nil @user.clocks.includes(:users, :runs > => :user_runs).find(@clock.id) > end > > should "find a clock with users order by users.name" do > assert_not_nil > @user.clocks.includes(:users).order("users.name").find(@clock.id) > end > > should "find a clock with users and runs order by users.name" do > assert_not_nil > @user.clocks.includes(:users, :runs).order("users.name").find(@clock.id) > end > > should "find a clock with users, runs and user_runs order by > users.name" do > assert_not_nil @user.clocks.includes(:users, :runs > => :user_runs).order("users.name").find(@clock.id) > end > end > > Every test but the last one pass. Is this not a bug? > > On Oct 7, 10:27 pm, J. Pablo Fernández <pup...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Continuing with my investigation I''ve tried this: > > > ubiquitous_user.clocks.includes(:runs > > => :user_runs).find_by_id(params[:id]) > > > and I''ve noticed the queries it''s generating doesn''t get user_runs at > > all. > > > On Oct 7, 9:51 pm, J. Pablo Fernández <pup...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > I have the following expression: > > > > user.clocks.includes(:runs > > > => :user_runs).includes(:users).find_by_id(params[:id]) > > > > which seems to work fine. But when I add an orders, like this: > > > > user.clocks.includes(:runs > > > => :user_runs).includes(:users).orders("users.names").find_by_id(params[:id]) > > > > it breaks with the following error: > > > > ActiveRecord::ConfigurationError: Association named ''user_runs'' > > > was not found; perhaps you misspelled it? > > > app/controllers/clocks_controller.rb:19:in `show'' > > > test/functional/clocks_controller_test.rb:21:in > > > `__bind_1286475263_942556'' > > > > Any ideas why? > > > > The model looks like this: > > > > class Clock < ActiveRecord::Base > > > has_and_belongs_to_many :users > > > has_many :runs > > > end > > > > class Run < ActiveRecord::Base > > > belongs_to :clock > > > has_many :user_runs > > > has_many :users, :through => :user_runs > > > end > > > > class UserRun < ActiveRecord::Base > > > belongs_to :run > > > belongs_to :user > > > end-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
radhames brito
2010-Oct-08 16:05 UTC
Re: Re: Not being able to order by includes on Rails3
Next time you have a problem with something like this use the to_sql method, it will show you the sql query generated, and you can see what is wrong with what you are doing. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.