Skip to content

Instantly share code, notes, and snippets.

@kennx
Created August 10, 2012 01:11
Show Gist options
  • Save kennx/3309945 to your computer and use it in GitHub Desktop.
Save kennx/3309945 to your computer and use it in GitHub Desktop.
# categorization.rb
class Categorization < ActiveRecord::Base
attr_accessible :category_id, :post_id
belongs_to :category
belongs_to :post
end
# category.rb
class Category < ActiveRecord::Base
attr_accessible :name
has_many :categorizations
has_many :posts, :through => :categorizations
end
# post.rb
class Post < ActiveRecord::Base
attr_accessible :title, :category_ids
has_many :categorizations
has_many :categories, :through => :categorizations
end
# view.html.erb
<div class="field">
<% Category.all.each do |c| %>
<br><%= check_box_tag 'post[category_ids][]', c.id,
@post.categories.include?(c), :id => "post_category_ids_#{c.id}" %> <%= label_tag "post_category_ids_#{c.id}", c.name %>
<% end %>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment