nufar
nufar

Reputation: 77

Rails 4 - belong_to nested object fields are not saved

Note: I've read a couple posts similar to this. But non of the solutions works for me.
I have two objects Appeal and Appealer with one to many relationship. When I save the Appeal object all fields are saved and also the appealer id is saved (as FK). But in the Appealer table a new record is saved with no data except for id.

MODEL

class Appeal < ActiveRecord::Base
    belongs_to :appealer, :autosave => true
    accepts_nested_attributes_for :appealer
end

class Appealer < ActiveRecord::Base
    has_many :appeals, :autosave => true
end

AppealsController

class AppealsController < ApplicationController
    def index
        respond_with Appeal.all
    end

    def create
        appealer = Appealer.create(appealer_params)
        @appeal = Appeal.create(appeal_params)
        @appeal.appealer = appealer


        if @appeal.save
          respond_with @appeal
        else
          respond_with {:error}
        end
    end

    def show
        respond_with Appeal.find(params[:id])
    end

    private
    def appeal_params
        params.require(:appeal).permit(:subject, :status, :submit_means, :card_type, :submit_date, :relationship, :report_date, :explanation, :email_approval)
    end
    def appealer_params
        params.require(:appeal).permit(appealer: [:appealer_id, :first_name, :last_name])
    end
end

EDIT Here is the json I use

{
        "id": 21,
        "subject": "axxxscaaaa",
        "status": "happy",
        "submit_means": "DOAR",
        "card_type": "sdsd",
        "submit_date": 1466629200000,
        "relationship": null,
        "report_date": 1466542800000,
        "explanation": "sdsd",
        "email_approval": null,
        "appealer": {"first_name":"aaaaaaa", "last_name":"fffff"},
        "selfRequest": false,
        "created_at": 1465851600000,
        "updated_at": 1465333200000
    }

I don't understand why the fields of appealer are not saved

Upvotes: 2

Views: 736

Answers (2)

Devd
Devd

Reputation: 343

  • As you JSON object showing, you are not using rails fields_for or something on view form. So you don't need this( accepts_nested_attributes_for :appealer ) line in your model. Model should be like this:

      class Appeal < ActiveRecord::Base
        belongs_to :appealer, :autosave => true
      end
    
  • Next thing, in your current logic,for your appealer_params, change appealer_params method with this:

    def appealer_params
      params.require(:appealer).permit(:first_name, :last_name)
    end
    
  • Create action logic for your scenario:

    def create
      @appeal = Appeal.new(appeal_params)
      if appealer_params.present?
        appealer = Appealer.create(appealer_params)      
        @appeal.appealer = appealer
      end
    
      if @appeal.save
        respond_with @appeal
      else
        respond_with {:error}
      end
    end
    

Upvotes: 2

Anna88
Anna88

Reputation: 355

Try this

def appeal_params
   params.require(:appeal).permit(:subject, :status, :submit_means, :card_type, :submit_date, :relationship, :report_date, :explanation, :email_approval, appealers_attributes: [:id, :first_name, :last_name] )
end

and remove this line appealer = Appealer.create(appealer_params)

Upvotes: 0

Related Questions