Buildings have units. Units have rooms. Rooms have people. I can grab building, room, and people IDs and store them in a session[:variable]. However, when creating a new unit, I need to give that session[:building_id] to ActiveRecord for creation. I''m not sure whether to use hidden fields or do it all in the model ''before_validation'' - in either case I don''t know how. My attempt failed with an ''Invalid method error'': before_validation :set_building_id def set_building_id @unit.building_id = session[:building_id] end How can I add session variables to insert/create statements? -- 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:> > before_validation :set_building_id > > def set_building_id > @unit.building_id = session[:building_id] > end > > How can I add session variables to insert/create statements?I use hidden fields for this kind of thing, but don''t know if it is the right way! -- 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 -~----------~----~----~----~------~----~------~--~---
uhm sorry i don''t get the actual problem, but before_filter surely is the wrong place, plus in your above code @unit wasnt initialized in any way, so its no ARObject of the Uni Model and has no method/attribute "building_id" class Unitcontroller < ... def create @unit = Unit.new(params[:unit]) # if you have a hidden field with name="unit[building_id]", the building_id will be assigned with this command too. @unit.building_id = session[:building_id] # You can assign like this from the session., if you dont want to use a hidden field. @unit.save end end Hidden fields are more common for this type of thing imho.. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> class Unitcontroller < ... > > def create > @unit = Unit.new(params[:unit]) > @unit.building_id = session[:building_id] > @unit.save > end > endThat worked great. Thanks! -- 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 -~----------~----~----~----~------~----~------~--~---