I can not update nested attributes which is related with current model by 3rd model.
Focused model: Profile
class Profile < ApplicationRecord
belongs_to :user
has_many :phone_numbers
##Set nested attributes
accepts_nested_attributes_for :phone_numbers, reject_if: :all_blank, allow_destroy: true
Netsted Attributes: PhoneNumber
class PhoneNumber < ApplicationRecord
belongs_to :profile
end
3rd model: User
class User < ApplicationRecord
has_one :profile
end
In database their relation is profile.user_id = user.id, phone_number.user_id = user.id
Question: How can I update Phone numbers when I update profile?
I tried
<%= form_for @profile, url: {action: "update"} do |f| %>
...
<%= f.fields_for :phone_numbers do |ff| %>
...
and got error message:
Mysql2::Error: Unknown column 'phone_numbers.profile_id' in 'where clause': SELECT
phone_numbers
.* FROMphone_numbers
WHEREphone_numbers
.profile_id
= 1
The errors is super clear and full of call to action:
profile_id
column to phone_numbers
table to reflect the association between Profile
and PhoneNumber
models.