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?
The second code snippet should be:
session[:current_room_id] = Person.find(session[:person_id]).rooms
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 | [email protected]
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 | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
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?
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?
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
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