Skip to content
Andrew Geweke edited this page Dec 13, 2013 · 1 revision

Just like an ActiveRecord model, you can declare methods inside a flex_column declaration. (A flex_column declaration does, in fact, create a new class, and flex-column fields are accessed via an instance of this class.) For example:

class User < ActiveRecord::Base
  flex_column :user_attributes do
    field :maximum_emails_per_week
    field :last_seen_at

    def seen!
      self.last_seen_at = Time.now
    end

    def seen_in_last_day?
      last_seen_at && last_seen_at >= 1.day.ago
    end
  end
end

my_user = User.find(...)
my_user.seen!
my_user.user_attributes.seen_in_last_day?

By default, such methods are delegated from the model class automatically, and hence present on instances of that class. If you pass :delegate => false in the declaration of the flex column, this will not happen, and you'll only be able to access such methods through the flex-column object itself. See controlling method delegation for more information.

Clone this wiki locally