Skip to content

Instantly share code, notes, and snippets.

@caike
Created January 23, 2011 15:42
Show Gist options
  • Save caike/792152 to your computer and use it in GitHub Desktop.
Save caike/792152 to your computer and use it in GitHub Desktop.
serializing preferences
# in the user form partial
<h3>Preferences</h3>
<% User::PREFERENCES.each do |preference| %>
<div class="field">
<%= check_box_tag "user[preferences[#{preference.to_s}]]", "1", @user.preferences ? @user.preferences[preference] : false %>
<%= label_tag :attribute, preference.to_s.titleize %>
</div>
<% end %>
class User < ActiveRecord::Base
has_many :tweets
has_one :account_setting, :dependent => :destroy
accepts_nested_attributes_for :account_setting
validates_presence_of :first_name, :last_name, :email
validates_uniqueness_of :email
PREFERENCES = [:notify_retweet, :notify_direct_messages, :notify_new_follower]
serialize :preferences, Hash
def to_s
"#{first_name} #{last_name}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment