No commit activity in last 3 years
No release in over 3 years
Serialize model attributes to a single database column instead
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.0.7
~> 1.5.2

Runtime

~> 3.0.0
 Project Readme

serialized_attributes

This is a very cool lib, allows to define dynamic serialized fields in your AR model, which acts as normal rails db columns. It give you full power of rails to your virtual columns: validations, rails forms, error messages, type casting (integer, date/time), type safety, :dirty? s, etc.. STI is supported! Basic associations is working too!

Now updated for Rails 3

Example of usage

STI table def

create_table :documents do |t|
  t.string :type
  t.text :serialized_attributes     # <---  here all your dynamic fields will be saved
  t.integer :reference_id           # <---  you can also define any sql columns for your indexes
  t.timestamps
end

STI base model

class Document < ActiveRecord::Base
  include SerializedAttributes
end

Other models..

class Post < Document
  attribute :title, String
  attribute :body,  String
  attribute :is_published, Boolean, :default => false
  
  attribute :comment_ids, Array             # <--- serialized Array of ids of associated comments
  has_references_to :comments
  
  validates_presence_of :title, :body
end

class Comment < Document
  attribute :body, String, :requied => true # <--- validates_presence_of :body
  attribute :post_id, Integer
  belongs_to :post
end

IRB fun

post = Post.create(:title => "First Post", :body => "Lorem ...")
assert !post.new_record?
post.comments = [Comment.create(:body => "this is a comment")]
post.comments << Comment.create(:body => "this is second comment")
assert_equal Comment.all.map(&:id), post.comment_ids
post.save
assert post.reload.comments.length == 2

Mass Assignment and Protect Attributes

class Widget < ActiveRecord::Base
  include SerializedAttributes

  #protect other attributes from mass assignment
  attr_accessible :name

	#specifically permit a given serialized attribute to be mass assigned
  accessible_attribute :creator, String
end

limitations

  • has-references-to association dont update reverse association
  • serialized-attributes column saved every time you call :save without depending on what is actually changed