No commit activity in last 3 years
No release in over 3 years
Serialize an array in a column, [] when no set, xx_as_text accessors and more.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

 Project Readme

Serialize an array in one column.

  • [] when not set
  • xxx_as_text / xxx_as_text= accessors to edit in forms
  • :on_set callback for cleanup
  • stores NULL when given array was empty (all empty -> IS NULL)

Install

  • As Rails plugin: rails plugin install git://github.com/grosser/ar_serialized_array.git
  • As gem: sudo gem install ar_serialized_array

Usage

# basic / forms
class User < ActiveRecord::Base
  serialized_array :product_ids, :accessor => :product_ids_as_text
end

<%= form_for @user do |f| %>
  <%= f.text_field :product_ids_as_text %>
<% end %>

# set / get
User.new.product_ids        # []

user.product_ids_as_text = "1, , 12323   , 23, 1"
user.product_ids            # [1, 12323, 23]
user.product_ids_as_text    # "1, 12323, 23"

# find ...
filled = User.all(:conditions => {:product_ids=>[1,3].to_yaml})
empty = User.all(:conditions => {:product_ids=>nil})

# with cleanup (unique integers)
class User < ActiveRecord::Base
  serialized_array :product_ids, :on_set=>lambda{|x| x.map(&:to_i).uniq }
end

TODO

  • add support for user.product_ids << 1

Author

Michael Grosser michael@grosser.it Hereby placed under public domain, do what you want, just do not hold me accountable...