So we have this model: class Post < ActiveRecord::Base belongs_to :person But when I join @post.person I only want the id and name. The docs fleetingly mention using :select to accomplish this but there are no examples and no explanations. I tried: belongs_to :person, :select => [:id, :name] belongs_to :person, :select => {:id, :name} ...without success. Could anyone explain how to use the :select parameter? Thanks! -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 3/8/07, Taylor Strait <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > So we have this model: > > class Post < ActiveRecord::Base > > belongs_to :person > > > But when I join @post.person I only want the id and name. The docs > fleetingly mention using :select to accomplish this but there are no > examples and no explanations. I tried: > > belongs_to :person, :select => [:id, :name] > belongs_to :person, :select => {:id, :name} > > ...without success. Could anyone explain how to use the :select > parameter? Thanks! > > -- > Posted via http://www.ruby-forum.com/. > > > >It''s a string, you''d need to do belongs_to :person, :select => "id AND name" Pat --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Does this not work with :include? Here''s my function: @posts = Post.find( :all, :conditions => "is_deleted = false", :order => "posts.created_at DESC", :include => :person) and the corresponding error: ArgumentError: Unknown key(s): select POST.RB class Post < ActiveRecord::Base belongs_to :person, :select => "id AND name" -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
First, :select defines a string that will go directly in to the SELECT * FROM * WHERE clause of the SQL statements ActiveRecord uses, so "id AND name" will NOT work. Second, in this particular case you probably shouldn''t be limiting the columns on the MODEL level. Instead, defining select for the find will do: @posts = Post.find( :all, :select => "id, name", :conditions => "is_deleted = false", :order => "posts.created_at DESC", :include => :person) On Mar 9, 4:24 am, Taylor Strait <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Does this not work with :include? Here''s my function: > > @posts = Post.find( :all, > :conditions => "is_deleted = false", > :order => "posts.created_at DESC", > :include => :person) > > and the corresponding error: > > ArgumentError: Unknown key(s): select > > POST.RB > class Post < ActiveRecord::Base > > belongs_to :person, :select => "id AND name" > > -- > Posted viahttp://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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Eden Li wrote:> First, :select defines a string that will go directly in to the SELECT > * FROM * WHERE clause of the SQL statements ActiveRecord uses, so "id > AND name" will NOT work. > > Second, in this particular case you probably shouldn''t be limiting the > columns on the MODEL level. Instead, defining select for the find > will do: > > @posts = Post.find( :all, > :select => "id, name", > :conditions => "is_deleted = false", > :order => "posts.created_at DESC", > :include => :person) >This doesn''t work, unfortunately. Does rails even pass :select with include? -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ooh.. yeah, it doesn''t. :include screws up the select portion of the query. Sorry, I missed that key in the list. Anyway, is there a reason to limit person to id/name? At this point, you can still do it, but you''ll have to use explicit SQL joins. @posts = Post.find( :all, :select => "posts.*, persons.id AS person_id, persons.name AS person_name", :join => "INNER JOIN #{Person.table_name} ON persons.id = posts.person_id", :conditions => "is_deleted = false", :order => "posts.created_at DESC") Then your @posts array will have a bunch of Post objects with all their attributes plus two new ones -- person_id and person_name. Using :include and referencing the Person model off the Post object is much cleaner though... On Mar 10, 3:13 am, Taylor Strait <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Eden Li wrote: > > First, :select defines a string that will go directly in to the SELECT > > * FROM * WHERE clause of the SQL statements ActiveRecord uses, so "id > > AND name" will NOT work. > > > Second, in this particular case you probably shouldn''t be limiting the > > columns on the MODEL level. Instead, defining select for the find > > will do: > > > @posts = Post.find( :all, > > :select => "id, name", > > :conditions => "is_deleted = false", > > :order => "posts.created_at DESC", > > :include => :person) > > This doesn''t work, unfortunately. Does rails even pass :select with > include? > > -- > Posted viahttp://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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Eden Li wrote:> Ooh.. yeah, it doesn''t. :include screws up the select portion of the > query. Sorry, I missed that key in the list. > > Anyway, is there a reason to limit person to id/name? At this point, > you can still do it, but you''ll have to use explicit SQL joins.The reason was just one of optimization - why load all the person data when all I need is the name and id? But its not worth writing nasty SQL for every :include! -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---