class UserAttribute include Mongoid::Document include Mongoid::Timestamps field :key has_many :attribute_values # Update or create the attribute_value records def attributes=(*args) args[0].each do |value| if value[:id].blank? attribute_values.build(value) else attribute_value = attribute_values.detect {|a| a.id.to_s == value[:id].to_s } attribute_value.update_attributes(value) end end end end