I have a Person model with attributes first_name and last_name. I want to implement autocomplete search that looks that the entire name. Hence I have a method: def full_name self.first_name + " " + self.last_name end Is it possible for me to query on full_name and have rails match the first_name and last_name fields? I currently receive this error when I try, because full_name isn''t an attribute in the database: SQLite3::SQLException: no such column: full_name: SELECT "people".* FROM "people" WHERE (LOWER(full_name) LIKE ''he %'') ORDER BY full_name ASC LIMIT 10 -- 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.
Marnen Laibow-Koser
2010-Oct-05 20:41 UTC
Re: Is it possible to query on virtual attributes?
Clay H. wrote:> I have a Person model with attributes first_name and last_name. > > I want to implement autocomplete search that looks that the entire > name. Hence I have a method: > > def full_name > self.first_name + " " + self.last_name > end > > Is it possible for me to query on full_name and have rails match the > first_name and last_name fields? > > I currently receive this error when I try, because full_name isn''t an > attribute in the database: > > SQLite3::SQLException: no such column: full_name: SELECT > "people".* FROM "people" WHERE (LOWER(full_name) LIKE ''he > %'') ORDER BY full_name ASC LIMIT 10There''s your answer. ActiveRecord find maps directly to database attributes. You could do something like :conditions => ["first_name like '':name%'' or last_name like '':name%''", {:name => "Smith"}], or define a computed column in a database view (in which case you will want the rails_sql_views plugin). Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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.
Walter Lee Davis
2010-Oct-06 01:30 UTC
Re: Re: Is it possible to query on virtual attributes?
In straight SQL you could order on the full name by using SELECT *, CONCAT(first_name, '' '', last_name) AS full_name FROM people ... ORDER BY full_name ASC. See if you can work that into a Rails finder syntax. Walter On Oct 5, 2010, at 4:41 PM, Marnen Laibow-Koser wrote:> Clay H. wrote: >> I have a Person model with attributes first_name and last_name. >> >> I want to implement autocomplete search that looks that the entire >> name. Hence I have a method: >> >> def full_name >> self.first_name + " " + self.last_name >> end >> >> Is it possible for me to query on full_name and have rails match the >> first_name and last_name fields? >> >> I currently receive this error when I try, because full_name isn''t an >> attribute in the database: >> >> SQLite3::SQLException: no such column: full_name: SELECT >> "people".* FROM "people" WHERE (LOWER(full_name) LIKE ''he >> %'') ORDER BY full_name ASC LIMIT 10 > > There''s your answer. ActiveRecord find maps directly to database > attributes. You could do something like :conditions => ["first_name > like '':name%'' or last_name like '':name%''", {:name => "Smith"}], or > define a computed column in a database view (in which case you will > want > the rails_sql_views plugin). > > Best, > -- > Marnen Laibow-Koser > http://www.marnen.org > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > -- > 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-/JYPxA39Uh5TLH3MbocFFw@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.