Move variables from session to ActiveRecord?

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?

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!

class Unitcontroller < …

def create
@unit = Unit.new(params[:unit])
@unit.building_id = session[:building_id]
@unit.save
end
end

That worked great. Thanks!

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…