Folks When im in rails 3.2 console i can do this just fine p = Person.last p.last_name and it prints the last name. But when i try to find it by the id, its able to locate the single record and store it in my variable p, but i cant print the last_name column. For example. p = Person.where(id: 34).limit(1) "printing p here shows all the columns" p.last_name says this NoMethodError: undefined method `last_name'' for #<ActiveRecord::Relation:0x000000055f8840> any help would be appreciated, 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-/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.
Person.where.limit returns a collection, you can to use .first, .last, .each. etc to access the element(s) or you can to use Person.find(34) in this case. 2012/5/18 brent brent <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>> Folks > When im in rails 3.2 console i can do this just fine > > p = Person.last > p.last_name > > and it prints the last name. > > But when i try to find it by the id, its able to locate the single > record and store it in my variable p, but i cant print the last_name > column. For example. > > > p = Person.where(id: 34).limit(1) > > "printing p here shows all the columns" > > p.last_name says this > > NoMethodError: undefined method `last_name'' for > #<ActiveRecord::Relation:0x000000055f8840> > > any help would be appreciated, 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-/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. > >-- Fernando Almeida www.fernandoalmeida.net -- 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.
There''s no reason to use ''where'' when you want to select by id since most of the times id is an unique value, and there''s no reason to use ''limit(1)'' if every time you select by id (SELECT * FROM users where id=34) it returns only one row, so you can use: p = People.find_by_id 34 # this will return nil if id doesn''t exits or p = People.find 34 # this will raise an exception if the id doesn''t exists and then use the object like this: p.last_name Otherwise, as Fernando pointed out, you''ll get a collection (a kind of Array) whose type is ActiveRecord::Relation. If you''re using the console you should realize that when you type "p = Person.where(id: 34).limit(1) " it returns something like: [#<Person id: 34, last_name: "the_last_name"......>] So you should treat it as an Array (actually as an Enumerable) that responds to :first, :last, :each, etc Since ActiveRecord::Relation includes the FinderMethods module ( https://github.com/rails/rails/blob/master/activerecord/lib/active_record/relation.rb#L19) which is located in https://github.com/rails/rails/blob/master/activerecord/lib/active_record/relation/finder_methods.rbyou can do: people = Person.where(id: 34).limit(1)> p = people.find 34 > p.last_nameHope that helps. On Friday, May 18, 2012 8:01:53 PM UTC-5, Ruby-Forum.com User wrote:> > Folks > When im in rails 3.2 console i can do this just fine > > p = Person.last > p.last_name > > and it prints the last name. > > But when i try to find it by the id, its able to locate the single > record and store it in my variable p, but i cant print the last_name > column. For example. > > > p = Person.where(id: 34).limit(1) > > "printing p here shows all the columns" > > p.last_name says this > > NoMethodError: undefined method `last_name'' for > #<ActiveRecord::Relation:0x000000055f8840> > > any help would be appreciated, 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 view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/eqEVnQ8oqQYJ. 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.