user4000617
user4000617

Reputation:

belongs_to with accepts_the_nested_attributes for is not saving foreign key in object

I have a model named Profile which has belongs_to relation with Address

class Profile < ActiveRecord::Base

 belongs_to :address, dependent: :destroy
 accepts_nested_attributes_for :address, allow_destroy: true

here is the code in controller

 def create
        @profile =  Profile.new(profile_signup_params)
        @profile.save
  respond_to
       ..... etc.....
 end

for params

def profile_signup_params
    params.require(:profile).permit( { address_attributes: [:country] 
end

but @profile.save

i get this object

#<MemberProfile:0x0000000af135b0
 id: 28,
 address_id: nil,
 birth_date: nil,
 country_code: nil,
 phone: nil,
 stripe_customer_id: "123",
 created_at: some time,
 updated_at: some time>

as you cane see this address_id is nil

Profile is created Address is created

but Address is not assigned to Profile

Please help me, what thing i am doing wrong

Upvotes: 3

Views: 300

Answers (2)

Richard Peck
Richard Peck

Reputation: 76774

To add to Vinay's answer (which is correct IMO), you'd want to make sure you're passing the right data through your controller.

Whilst the belongs_to association should allow you to set the nested parameters you require, it would be prudent to mention what Vinay said -- if you're creating an Address for a Profile, surely it would be the address that would belong to the Profile?

You can see about the has_one association here:

enter image description here


You'd handle it in a very similar way:

#app/controllers/profiles_controller.rb
class ProfilesController < ApplicationController
   def new
      @profile = Profile.new
      @profile.build_address
   end

   def create
      @profile = Profile.new profile_params
      @profile.save
   end

   private

   def profile_params
      params.require(:profile).permit(address_attributes: [:country])
   end
end

#app/views/profiles/new.html.erb
<%= form_for @profile do |f| %>
   <%= f.fields_for :address do |a| %>
      <%= a.text_field :country %>
   <% end %>
<% end %>

This, with the model code from Vinay should get it working properly.

Upvotes: 1

Gupta
Gupta

Reputation: 10358

I think You made a Wrong association between Address and Profile

Profile which has belongs_to relation with Address instead It should be Profile has has_one association with respect to Address.

As mention in official Documentation Active Record Nested Attributes

class Profile < ActiveRecord::Base

 has_one :address, dependent: :destroy
 accepts_nested_attributes_for :address, allow_destroy: true
...
end

class Address < ActiveRecord::Base
 belongs_to :profile
end

Rest Controller and model code would be same in your case except you need to a primary-foreign key relation between Address and Profile; need to create profile_id column in address table.

Note: Make sure if there is a uniqueness you need to follow Uniqueness Gotcha!!! in ActiveRecord NestedAttributes.

Original Blog Uniqueness Gotcha!!! Problem and Solution

Hope this This Help you!!!

Upvotes: 1

Related Questions