user5296896
user5296896

Reputation: 179

Rails 4, Saving arrays in Postgresql

I'm having problems saving my data of arrays into the database. Currently, I am using a nested form and have 3 models as shown below.

deal.rb

class Deal < ActiveRecord::Base
  has_many :deal_days, :dependent => :destroy
  accepts_nested_attributes_for :deal_days, allow_destroy: true
end


deal_day.rb

class DealDay < ActiveRecord::Base
  belongs_to :deal
  has_many :deal_times, :dependent => :destroy
  accepts_nested_attributes_for :deal_times, allow_destroy: true
end


deal_time.rb

class DealTime < ActiveRecord::Base
  belongs_to :deal_day
end

My objective is to allow users to select multiple days where the deal is ongoing in a checkbox. For example if a user selects Monday and Tuesday, I want the array to have monday and tuesday in it. Shown below is my current database for deal_day:

create_table "deal_days", force: true do |t|
    t.integer  "deal_id"
    t.string   "day",        default: [], array: true
    t.datetime "created_at"
    t.datetime "updated_at"
end

The form for deal_day is as follows in a file called _deal_day_fields:

<fieldset>
  Enter Days (e.g Mon - Fri)<br>
  <td><input type = "checkbox" name = "day[]" value = "Mon" /></td>
  <td><input type = "checkbox" name = "day[]" value = "Tue" /></td>
  <td><input type = "checkbox" name = "day[]" value = "Wed" /></td>
  <td><input type = "checkbox" name = "day[]" value = "Thur" /></td>
  <td><input type = "checkbox" name = "day[]" value = "Fri" /></td>
  <td><input type = "checkbox" name = "day[]" value = "Sat" /></td>
  <td><input type = "checkbox" name = "day[]" value = "Sun" /></td>
  <%= link_to "Delete", '#', class: "remove_fields" %><br>
  <%= f.hidden_field :_destroy %>
  <%= f.fields_for :deal_times do |builder| %>
      <%= render 'deal_time_fields', f: builder %>
  <% end %>
  <%= link_to_add_fields "Add another time period", f, :deal_times %>
</fieldset>

For the deal controller params, I have set it to be so:

    params.require(:deal).permit(:name_of_deal, :type_of_deal, :description, :start_date, :expiry_date, :location, :t_c,
                             :pushed,:redeemable, :multiple_use, :image,
                             deal_days_attributes: [:id, {day: []}, :_destroy,
                                                    deal_times_attributes: [:id, :started_at, :ended_at, :_destroy]])

When, I run the form, all my data is saved properly into the database except for the array of :day. It is saved into the database as [].
I would appreciate it if anyone can tell me my mistake. Thank you!

Upvotes: 3

Views: 1276

Answers (1)

rodamn
rodamn

Reputation: 2211

If you want days[] within deal_days in your controllers params as you posted in your strong parameters require/permit statement, you can build this with form helpers (see http://guides.rubyonrails.org/form_helpers.html#building-complex-forms).

Here is what it would look like:

# app/views/deals/_form.html.erb
    ...
    <%= f.fields_for :deal_days do |f_days| %>
        <%= fields_for 'deal[deal_days][]', f_days.object do |f_days_array| %>
            <%= f_days_array.check_box 'Monday' %>
            <%= f_days_array.check_box 'Tuesday' %>
            ...etc..
        <%= f.fields_for :deal_times do |builder| %>
            <%= render 'deal_time_fields', f: builder %>
        <% end %>
    <% end %>

Pretty sure that's it, or pretty close. I based this off of the code in section 7.3 of the Rails guide above. You should definitely read up on that whole page if you are still confused.

Caveat: This solution won't reach your stated objective, because this data is currently associated with the deal, while it needs to be associated with a user in addition to a specific deal/deal day. But that's another problem.

Upvotes: 1

Related Questions