jenson-button-event
jenson-button-event

Reputation: 18951

Clean Rails way to have a child collection linked by checkboxes

This is the thing

I have a table/class called Offer

And referenced table/class called OfferDay (because an Offer is available on x days of the week)

It's just a really simple question.

Is there a Ruby on Rails convention for editing such a thing?

My Offer admin form should have 7 checkboxes, prefilled with OfferDays for this instance (or not)

Yes of course, i can loop days of the week in my .erb and check the child collection (days) of the instance, but what about auto binding to instances on postbacK? - it seems the sort of legwork we've all seen before and should have a convention.

Yes I've googled... found sort of it, but not quite... any specific to this one?

Upvotes: 1

Views: 68

Answers (1)

Richard Peck
Richard Peck

Reputation: 76774

I'd handle it with a has_many relationship:

#app/models/offer.rb
Class Offer < ActiveRecord::Base
   has_many :days, -> { select(:day_id) }, class_name: "OfferDay", foreign_key: "offer_id"
   accepts_nested_attributes_for :days
end

Class OfferDay < ActiveRecord::Base
   belongs_to :offer
end

offer_days
id | offer_id | day_id #-> put indexes on day_id & offer_id

You'll be able to call @offer.days to reveal the days on which your offer is running. It will of course return a number, which you'll then translate into a day using the Date class or similar


Editing

This will allow you to assign any number of days to each offer (and each of the days being unique). I'd update like this:

#app/controllers/offers_controller.rb
def new
   @offer = Offer.new
   @offer.days.build
end

def create
   @offer = Offer.new(offer_params)
   @offer.save
end

private

def offer_params
    params.require(:offer).permit(:offer, :attributes, days_attributes: [:day_id])
end

#app/views/offers/new.html.erb
<%= form_for @offer do |f| %>
   <%= f.text_field :offer %>
   <%= f.text_field :attributes %>
   <%= f.fields_for :days do |builder| %>
       <% 7.times do |day| %>
          <%= builder.check_box :day_id, day %> #-> need label for each day
       <% end %>
   <% end %>
<% end %>

Upvotes: 1

Related Questions