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.idreturns 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.idreturns 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: userThough 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? -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
The second code snippet should be:> session[:current_room_id] = Person.find(session[:person_id]).rooms-- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Nov-14 21:35 UTC
Re: Selecting ''attribute id'' not ''object id''
HI -- On Tue, 14 Nov 2006, Taylor Strait wrote:> > 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 -- David A. Black | dblack-0o/XNnkTkwhBDgjK7y7TUQ@public.gmane.org Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB''s Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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? -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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.idMaybe using a model table for joins just has to be that messy in order to get the added functionality? -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Taylor Strait 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 -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> If you know there is only one room, > > Person.find(session[:person_id]).rooms[0].id> --Al EvansThat 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-- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---