HI --
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 <SNIP> 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?
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