# Selecting 'attribute id' not 'object id'

**URL:** <https://www.ruby-forum.com/t/selecting-attribute-id-not-object-id/75987>\
**Category:** Rails\
**Created:** [November 14, 2006, 10:31pm UTC](https://www.ruby-forum.com/t/selecting-attribute-id-not-object-id/75987 "2006-11-14T22:31:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [November 14, 2006, 10:31pm UTC](https://www.ruby-forum.com/t/selecting-attribute-id-not-object-id/75987/1 "2006-11-14T22:31:09Z")

</div>

When a user logs in I want to grab the room he lives in by its id but I  
can’t seem to get only the room id!

> session[:current\_room\_id] = Person.find(session[:person\_id]).rooms.id

returns this for ‘session’:

> — !ruby/object:CGI::Session  
> data: &id001  
> :person\_id: 1  
> :person\_name: Test User  
> :current\_room\_id: 57306370 \<–ARRAY OBJECT ID  
> :person\_role: user  
> flash: !map:ActionController::Flash::FlashHash {}

But I don’t want to the object id of the room array, I want the specific  
Room.id so I can call it back with the session when I need it. Taking  
out’.id’ returns an object array:

> session[:current\_room\_id] = Person.find(session[:person\_id]).rooms.id

returns this for ‘session’:

> — !ruby/object:CGI::Session  
> data: &id001  
> :current\_room\_id:
> 
> - !ruby/object:Room  
> attributes:  
> id: 1 \<–THIS IS WHAT I WANT  
>   
> width: “25”  
> :person\_id: 1  
> :person\_name: Test User  
> :current\_room: 57306370  
> :person\_role: user

Though I’m thrilled that I was able to pull off the  
‘Person.find(session[:person\_id]).rooms’ join on my first try, I’m sure  
I am missing some small detail to grab the room id. Any ideas?

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [November 14, 2006, 10:31pm UTC](https://www.ruby-forum.com/t/selecting-attribute-id-not-object-id/75987/2 "2006-11-14T22:31:55Z")

</div>

The second code snippet should be:

> session[:current\_room\_id] = Person.find(session[:person\_id]).rooms

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [November 14, 2006, 10:36pm UTC](https://www.ruby-forum.com/t/selecting-attribute-id-not-object-id/75987/3 "2006-11-14T22:36:11Z")

</div>

HI –

On Tue, 14 Nov 2006, Taylor S. wrote:

> > :person\_id: 1
> 
> > :person\_id: 1  
> > :person\_name: Test User  
> > :current\_room: 57306370  
> > :person\_role: user
> 
> Though I’m thrilled that I was able to pull off the  
> ‘Person.find(session[:person\_id]).rooms’ join on my first try, I’m sure  
> I am missing some small detail to grab the room id. Any ideas?

If the person only has one room:

class Person \< ActiveRecord::Base  
has\_one :room

and

class Room \< ActiveRecord::Base  
belongs\_to :person

then you should be able to do:

session[:current\_room\_id] = Person.find(session[:person\_id]).room.id

If the person has multiple rooms, then you have to decide some way to  
decide which one you want to save the id of, out of the array of all  
of them.

David

–  
David A. Black | removed\_email\_address@domain.invalid  
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
[1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
[2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [November 14, 2006, 11:15pm UTC](https://www.ruby-forum.com/t/selecting-attribute-id-not-object-id/75987/4 "2006-11-14T23:15:14Z")

</div>

The issue seems to be the model-join table ‘rentals.’ for people -  
rooms. With my textbook has\_many \<-\> belongs\_to relationships:

Buildings have many units  
Units have many rooms

ActiveRecord pulls the ids easily:

> Person.find(session[:person\_id]).rooms.each {|room| session[:current\_room\_id] = room.id}  
> session[:current\_unit\_id] = Room.find(session[:current\_room\_id]).unit.id  
> session[:current\_building\_id] = Unit.find(session[:current\_unit]).building.id

Maybe using a model table for joins just has to be that messy in order  
to get the added functionality?

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [November 14, 2006, 11:06pm UTC](https://www.ruby-forum.com/t/selecting-attribute-id-not-object-id/75987/5 "2006-11-14T23:06:53Z")

</div>

Thanks David, this solved the issue:

> Person.find(session[:person\_id]).rooms.each {|room| session[:current\_room\_id] = room.id}

However, I’m using a third table ‘rentals’ to join people and rooms  
therefore have had to use “has\_many” because “has\_one” does NOT support  
‘:through =\>’. So in reality a person cannot have more than one room  
but, since I am keeping historical data, but in the table a person will  
have multiple rooms. Any join will only return one result though  
because I have set this in the models:

> has\_many :people, :through =\> :rentals, :conditions =\> “ended\_at IS NULL”  
> has\_many :rooms, :through =\> :rentals, :conditions =\> “ended\_at IS NULL”

So even though there will only be one room in the  
Person.find(session[:person\_id]).rooms array do I still have to use an  
iterator? Is there not a prettier way to do this?

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [November 14, 2006, 11:35pm UTC](https://www.ruby-forum.com/t/selecting-attribute-id-not-object-id/75987/6 "2006-11-14T23:35:17Z")

</div>

Taylor S. wrote:

> So even though there will only be one room in the  
> Person.find(session[:person\_id]).rooms array do I still have to use an  
> iterator? Is there not a prettier way to do this?

If you know there is only one room,

Person.find(session[:person\_id]).rooms[0].id

should work.

–Al Evans

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [November 15, 2006, 12:31am UTC](https://www.ruby-forum.com/t/selecting-attribute-id-not-object-id/75987/7 "2006-11-15T00:31:16Z")

</div>

> If you know there is only one room,
> 
> Person.find(session[:person\_id]).rooms[0].id

> –Al Evans

That worked like a charm. I just didn’t know how to reference the array  
properly. Thanks! Isn’t this better:

> session[:current\_room\_id] = Person.find(session[:person\_id]).rooms[0].id  
> session[:current\_unit\_id] = Room.find(session[:current\_room\_id]).unit.id  
> session[:current\_building\_id] = Unit.find(session[:current\_unit]).building.id
