Accessing results of retrieve where

12 views
Skip to first unread message

Dave Castellano

unread,
Jan 29, 2014, 10:13:23 PM1/29/14
to rubyonra...@googlegroups.com
Hi,

I am searching for a record using "where":

@profile = Profile.where(:user_id => current_user.id)

This returns:
=> [#<Profile id: 6, user_id: 19, last_subject_id: 6, last_book_id: 2,
last_chapter_id: 1, last_section_id: 1, last_subsection_id: 1,
last_minisection_id: 20, about: nil, image: nil, created_at: "2013-03-13
01:45:44", updated_at: "2014-01-30 02:11:54", students_last_subject_id:
6, students_last_book_id: nil, students_last_chapter_id: nil>]

I am trying to then access "students_last_subject_id" using several
methods including @profile.students_last_subject_id,
but it keeps returning nil. With the enclosing brackets i am guessing
this is not a hash..

Can anyone help?

Thanks,

Dave

--
Posted via http://www.ruby-forum.com/.

Dave Castellano

unread,
Jan 29, 2014, 10:18:35 PM1/29/14
to rubyonra...@googlegroups.com
Hi,

I am searching for a record using "where":

@profile = Profile.where(:user_id => current_user.id)

This returns:
=> [#<Profile id: 6, user_id: 19, last_subject_id: 6, last_book_id: 2,
last_chapter_id: 1, last_section_id: 1, last_subsection_id: 1,
last_minisection_id: 20, about: nil, image: nil, created_at: "2013-03-13
01:45:44", updated_at: "2014-01-30 02:11:54", students_last_subject_id:
6, students_last_book_id: nil, students_last_chapter_id: nil>]

I am trying to then access "students_last_subject_id" using several
methods including @profile.students_last_subject_id,
but it keeps returning nil. With the enclosing brackets i am guessing
this is not a hash, but I also cannot access it as an array .

tamouse pontiki

unread,
Jan 29, 2014, 10:30:52 PM1/29/14
to rubyonra...@googlegroups.com
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/536937e965138f93933c40f8e3f2df68%40ruby-forum.com.
For more options, visit https://groups.google.com/groups/opt_out.

Nope. where returns an array containing all the records it finds, even if it's only one. If you know, or at least are pretty sure, your search will only give one row object, you can use find_by, or just pull the first element from the where's array:

@profile = Profile.find_by(:user_id => current_user.id)

@profile = Profile.where(:user_id => current_user.id).first


Walter Lee Davis

unread,
Jan 30, 2014, 1:04:49 AM1/30/14
to rubyonra...@googlegroups.com
Where always returns an array (sometimes it's empty). If you want to get the record (or nil) you can use Profile.find(current_user.id) instead. That will return the bare object (not wrapped in an array) and you can get the attributes out of it. If you want to use where(), then do this:

@profile = Profile.where(:user_id => current_user.id).first

Walter

>
> Thanks,
>
> Dave
>
> --
> 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 unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/20d1e13abaf5242ca87537ed4a3a31c5%40ruby-forum.com.

Colin Law

unread,
Jan 30, 2014, 4:17:28 AM1/30/14
to rubyonra...@googlegroups.com
Also, assuming you have setup your relationships correctly (so profile
belongs_to user, user has_many profiles) then you should be able to do
@profile = current_user.profiles.first
or if you have user has_one profile then
@profile = current_user.profile

Colin

Dave Castellano

unread,
Jan 30, 2014, 5:53:26 PM1/30/14
to rubyonra...@googlegroups.com
Thanks!!
Reply all
Reply to author
Forward
0 new messages