Last Updated: November 09, 2017
·
713
· tmartin314

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

1 Response
Add your response

Cooooool

over 1 year ago ·