# Move variables from session to ActiveRecord?

**URL:** <https://www.ruby-forum.com/t/move-variables-from-session-to-activerecord/75477>\
**Category:** Rails\
**Created:** [November 10, 2006, 6:04am UTC](https://www.ruby-forum.com/t/move-variables-from-session-to-activerecord/75477 "2006-11-10T06:04:13Z")\
**Posts on this page:** 4\
**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 10, 2006, 6:04am UTC](https://www.ruby-forum.com/t/move-variables-from-session-to-activerecord/75477/1 "2006-11-10T06:04:13Z")

</div>

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?

---

<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 10, 2006, 6:19am UTC](https://www.ruby-forum.com/t/move-variables-from-session-to-activerecord/75477/2 "2006-11-10T06:19:35Z")

</div>

Taylor S. 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!

---

<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 10, 2006, 4:25pm UTC](https://www.ruby-forum.com/t/move-variables-from-session-to-activerecord/75477/3 "2006-11-10T16:25:47Z")

</div>

> class Unitcontroller \< …
> 
> def create  
> @unit = Unit.new(params[:unit])  
> @unit.building\_id = session[:building\_id]  
> @unit.save  
> end  
> end

That worked great. Thanks!

---

<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 10, 2006, 9:15am UTC](https://www.ruby-forum.com/t/move-variables-from-session-to-activerecord/75477/4 "2006-11-10T09:15:33Z")

</div>

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…
