I have a user who can have many reservations and each reservation can have many tables. It saves the user and the reservations, but not the tables. Tables can have special requirements, like wheelchair accessible or kids high chair etc.
Models
User:
class User < ApplicationRecord
has_many :reservations, dependent: :destroy
has_many :tables, through: :reservations
accepts_nested_attributes_for :reservations, reject_if: :all_blank, allow_destroy: true
accepts_nested_attributes_for :tables, reject_if: :all_blank, allow_destroy: true
end
Reservations:
class Reservation < ApplicationRecord
belongs_to :user
has_many :tables
accepts_nested_attributes_for :tables, reject_if: :all_blank, allow_destroy: true
end
Tables:
class Table < ApplicationRecord
belongs_to :reservation
end
The form structure looks like this:
Views
user/_form.html.slim
= simple_form_for(@user) do |f|
.row
.col-6
= f.input :last_name, required: true
.row
.col-6
= f.input :first_name
.col-6
= f.input :email
... some more here
= f.simple_fields_for :reservations do |res|
= render 'reservations/reservations_fields', f: res
.links
= link_to_add_association 'add reservation', f, :reservations, partial: 'reservations/reservations_fields'
reservations/_reservations_fields.html.slim:
.form-inputs
.row
.col-6
= f.input :reservation_datetime, as: :datetime, minute_step: 15
... some more here
.row
= f.simple_fields_for :tables do |table|
.col-12
= render 'tables/form', f: table
tables/_form.html.slim:
.nested-fields
.form-inputs
.row
.col-6
= f.input :wheelchair
... etc
Controller
users_controller.rb
def user_params
params.require(:user).permit(:first_name, :last_name, :phone_number, :email, :company, :comments,
reservations_attributes: [:id, :reservation_datetime, :important, :head_count, :comments,
tables_attributes: [:id, :wheelchair, :children, :comments]])
end
Why are the table attributes not saving? I cannot find the mistake.
Edit: I want to add that it saves the association, but NOT the attributes. Thank you
Edit 2: The params are sent to the UserController (I checked with byebug), but they don't get saved.
byebug:
(byebug) user_params
<ActionController::Parameters {[...], "comments"=>"", "reservations_attributes"=><ActionController::Parameters {"0"=><ActionController::Parameters { [...], "tables_attributes"=><ActionController::Parameters {"0"=><ActionController::Parameters {"wheelchair"=>"0", "children"=>"2", "comments"=>""} permitted: true>} permitted: true>} permitted: true>} permitted: true>} permitted: true>
But the SQL statement is
↳ app/controllers/users_controller.rb:42:in `create'
Tables Create (1.0ms) INSERT INTO "tables" ("reservation_id") VALUES ($1) RETURNING "id" [["reservation_id", 12]]
↳ app/controllers/users_controller.rb:42:in `create'
(2.0ms) COMMIT
Alright, the problem wasn't because it was third-level nested. The real problem was that I used attr_accessor
in my tables model. When I removed this, it suddenly worked. Don't know why though, will open another question regarding this.