Jeff Jeff - 1 year ago 108
Ruby Question

Rails model -- non persistent class member or property?

Is it possible / advisable to have a member of a class that is not persisted to the database for a rails model?

I want to store the last type the user selects in a session variable. Since I cant set the session variable from my model, I want to store the value in a "dummy" class member that just passes the value back to the controller.

Can you have such a class member?

max max
Answer Source

Adding non-persisted attributes to a Rails model is just like any other Ruby class:

class User < ActiveRecord::Base
   attr_accessor :someattr

me = 'Max', someattr: 'bar')
me.someattr  # "bar"
me.someattr = 'foo'

The extended explanation:

In Ruby all instance variables are private and do not need to be defined before assignment.

attr_accessor creates a setter and getter method:

class User < ActiveRecord::Base

   def someattr

   def someattr=(value)
     @someattr = value

There is one special thing going on here; Rails takes the hash you pass to and maps the values to attributes. You could simulate this behavior in a plain ruby class with something like:

class Foo

  attr_accessor :bar

  def initialize(hash)
    hash.keys.each do |key|
      setter = "#{key}=".intern
      self.send(setter, hash[key]) if self.respond_to? setter

> 'baz')
=> <Foo:0x0000010112aa50 @bar="baz">

Classes in Ruby can also be re-opened at any point, ActiveRecord uses this ability to "auto-magically" add getters and setters to your models based on its database columns (ActiveRecord figures out which attributes to add based on the database schema).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download