Rails 4 Join Table Config with Form Helpers
We needed to associate one model with another and uncovered some cool built in tools for Rails 4.
Connect your models through a join table
There's a nice post here that summarizes this process:
http://nishacodes.tumblr.com/post/73484141822/join-tables-heres-the-deal
class Model < ActiveRecord::Base
has_many :stages, :through => :model_stages
has_many :model_stages
end
class Stage < ActiveRecord::Base
has_many :models, :through => :model_stages
has_many :model_stages
end
class ModelStages < ActiveRecord::Base
belongs_to :model
belongs_to :stage
end
Add collection form helper (using slim)
= f.collection_check_boxes :stage_ids, Stage.all, :id, :name do |s|
label.col-md-4
= s.check_box(class: "checkbox checkbox-primary")
span = s.text
Written by Troy Martin
Related protips
1 Response
Cooooool
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Rails
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#