comopasta Gr
2010-Nov-18 21:13 UTC
Rails 3 ActiveRecord queries - I''m missing something very BIG
Hi, I know I am missing something very big regarding the changes with respect to activerecord in Rails 3. I can''t find the explanation. And I''m sure someone will kick my ass for not finding the right piece of info in the docs and guides. Please do. I have a working piece of code but I don''t like it. So. Very basic association: class Project < ActiveRecord::Base has_many :tasks end class Task < ActiveRecord::Base belongs_to :project end The task table has the foreign key: t.integer "project_id" I can create a task this way, and the resulting task has the correct project id stored @project = Project.find(1) @task = @project.tasks.build(params[:task]) @task.save Now I get the project record using the Rails 2 way: @project = Project.find(1) triggers: Project Load (0.2ms) SELECT `projects`.* FROM `projects` WHERE (`projects`.`id` = 1) LIMIT 1 @project.inspect outputs: #<Project id: 1, name: "test", created_at: "2010-11-18 15:15:23", updated_at: "2010-11-18 15:15:23"> @project.tasks.inspect shows: [#<Task id: 1, title: "test", project_id: 1, created_at: "2010-11-18 15:20:06", updated_at: "2010-11-18 15:20:06">] But now I want to use the Rails 3 way: @proj = Project.where(:id => params[:id]) trigger: Project Load (0.3ms) SELECT `projects`.* FROM `projects` WHERE (`projects`.`id` = 1) @proj.inspect shows: [#<Project id: 1, name: "test", created_at: "2010-11-18 15:15:23", updated_at: "2010-11-18 15:15:23">] And @proj.tasks.inspect gives: NoMethodError (undefined method `tasks'' for #<ActiveRecord::Relation:0x00000103922f30>): Now, if I use: @proj[0].tasks.inspect then I get the tasks for the project. Is this the right way? I have the feeling it is not. Cheers. -- Posted via http://www.ruby-forum.com/. -- 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.
Philip Hallstrom
2010-Nov-18 21:39 UTC
Re: Rails 3 ActiveRecord queries - I''m missing something very BIG
On Nov 18, 2010, at 1:13 PM, comopasta Gr wrote:> Hi, > > I know I am missing something very big regarding the changes with > respect to activerecord in Rails 3. > > I can''t find the explanation. And I''m sure someone will kick my ass for > not > finding the right piece of info in the docs and guides. Please do. > > I have a working piece of code but I don''t like it. > > So. Very basic association: > > class Project < ActiveRecord::Base > has_many :tasks > end > > class Task < ActiveRecord::Base > belongs_to :project > end > > > The task table has the foreign key: > t.integer "project_id" > > > I can create a task this way, and the resulting task has the correct > project id stored > > @project = Project.find(1) > @task = @project.tasks.build(params[:task]) > @task.save > > > Now I get the project record using the Rails 2 way: > > @project = Project.find(1) triggers: > Project Load (0.2ms) SELECT `projects`.* FROM `projects` WHERE > (`projects`.`id` = 1) LIMIT 1 > > @project.inspect outputs: > #<Project id: 1, name: "test", created_at: "2010-11-18 15:15:23", > updated_at: "2010-11-18 15:15:23"> > > @project.tasks.inspect shows: > [#<Task id: 1, title: "test", project_id: 1, created_at: "2010-11-18 > 15:20:06", updated_at: "2010-11-18 15:20:06">] > > But now I want to use the Rails 3 way: > > @proj = Project.where(:id => params[:id]) trigger: > Project Load (0.3ms) SELECT `projects`.* FROM `projects` WHERE > (`projects`.`id` = 1) > > @proj.inspect shows: > [#<Project id: 1, name: "test", created_at: "2010-11-18 15:15:23", > updated_at: "2010-11-18 15:15:23">]where() is returning an array of objects. That''s not technically accurate, but once you ask for the result of where() using all or inspect or anything else, it gets turned into an array. There''s nothing wrong with writing Project.find(params[:id]) for finding a specific instance...> > And @proj.tasks.inspect gives: > NoMethodError (undefined method `tasks'' for > #<ActiveRecord::Relation:0x00000103922f30>): > > > Now, if I use: > @proj[0].tasks.inspect then I get the tasks for the project. > > Is this the right way? I have the feeling it is not. > > Cheers. > > -- > Posted via http://www.ruby-forum.com/. > > -- > 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. >-- 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.
comopasta Gr
2010-Nov-18 22:24 UTC
Re: Rails 3 ActiveRecord queries - I''m missing something very BIG
Thanks Philip! Yeah I wanted to include the tasks while getting the project. I think this does it Rails 3 way: @proj = Project.includes(:tasks).find_by_id(params[:id]) @proj.tasks returns the array of tasks and looks much better. Cheers. -- Posted via http://www.ruby-forum.com/. -- 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.
Colin Law
2010-Nov-19 08:57 UTC
Re: Re: Rails 3 ActiveRecord queries - I''m missing something very BIG
On 18 November 2010 22:24, comopasta Gr <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Thanks Philip! > > Yeah I wanted to include the tasks while getting the project. > > I think this does it Rails 3 way: > @proj = Project.includes(:tasks).find_by_id(params[:id]) > > @proj.tasks returns the array of tasks and looks much better.Just a note to point out that the .includes(:tasks) is not required. @proj.tasks will still work without it (unless this has changed in rails 3). Using includes will reduce the number of hits on the db however. Colin -- 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.
Frederick Cheung
2010-Nov-19 10:18 UTC
Re: Rails 3 ActiveRecord queries - I''m missing something very BIG
On Nov 19, 8:57 am, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 18 November 2010 22:24, comopasta Gr <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > > > Thanks Philip! > > > Yeah I wanted to include the tasks while getting the project. > > > I think this does it Rails 3 way: > > @proj = Project.includes(:tasks).find_by_id(params[:id]) > > > @proj.tasks returns the array of tasks and looks much better. > > Just a note to point out that the .includes(:tasks) is not required. > @proj.tasks will still work without it (unless this has changed in > rails 3). Using includes will reduce the number of hits on the db > however.Except that when loading a single record it''s pretty pointless: a second query is used to load the tasks, so there isn''t much point of doing it ahead of time (of course if you were loading 20 projects it would be a different matter) Fred -- 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.
Marnen Laibow-Koser
2010-Nov-19 12:15 UTC
Re: Rails 3 ActiveRecord queries - I''m missing something very BIG
Frederick Cheung wrote in post #962548:> On Nov 19, 8:57am, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >> >> Just a note to point out that the .includes(:tasks) is not required. >> @proj.tasks will still work without it (unless this has changed in >> rails 3). Using includes will reduce the number of hits on the db >> however. > > Except that when loading a single record it''s pretty pointless: a > second query is used to load the tasks, so there isn''t much point of > doing it ahead of time (of course if you were loading 20 projects it > would be a different matter)In other words, it sounds like (as in Rails 2) it''s generally better to use joins than includes. Not to hijack the thread, but: what''s the point of includes? Why make a separate query when the DB could do it in one query with a join? I''ve never understood this. What, if anything, am I missing?> > FredBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org Sent from my iPhone -- Posted via http://www.ruby-forum.com/. -- 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.
comopasta Gr
2010-Nov-19 14:45 UTC
Re: Rails 3 ActiveRecord queries - I''m missing something very BIG
Hi, Thanks for the comments. Yeah :include would make only when handling some amount of data to include. No much point if it is only a single task. I checked with Mr. Bates and got a lesson about all this. For Rails 2 but anyway the principle should be the same in Rails 3. http://asciicasts.com/episodes/181-include-vs-joins So as some of you pointed out joins is a better option in most of the cases, since you can pretty much achieve the same and gain more control. Regards. -- Posted via http://www.ruby-forum.com/. -- 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.
Colin Law
2010-Nov-19 15:51 UTC
Re: Re: Rails 3 ActiveRecord queries - I''m missing something very BIG
On 19 November 2010 14:45, comopasta Gr <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi, > > Thanks for the comments. Yeah :include would make only when handling > some amount of data to include. No much point if it is only a single > task. > > I checked with Mr. Bates and got a lesson about all this. For Rails 2 > but anyway the principle should be the same in Rails 3. > > http://asciicasts.com/episodes/181-include-vs-joins > > So as some of you pointed out joins is a better option in most of the > cases, since you can pretty much achieve the same and gain more control.However I think that in your example @proj = Project.find_by_id(params[:id]) @tasks = @proj.tasks neither is necessary. Joins will do nothing for you, includes will provide a small efficiency improvement over not including. Colin -- 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.