0

agent.rb

has_many :organisations

Organisation.rb

belongs_to :agent

organisations_agent.rb

validates :organisation_id, uniqueness: { scope: [:agent_id] }

organisations_controller.rb

def create
    @organisation = current_agent.organisations.build(organisation_params)
    @organisation.save
end
private

def organisation_params
  params.require(:organisation).permit(:name, :organisation_size, :area, :management)
end

I have the following associations as above. The issue I am facing here is that in the create action when I try to create an organisation I am getting an error that agent_id is blank because of which build doesn't save the record. Please help me find where I am going wrong.

I added binding.pry_remote right before the @organisation.save and I checked that in @organisation the agent_id is nil. So I checked if there is actually a current_agent in the create agent and yes that works fine. I am getting current_agent in the create action. I don't know where I am going wrong but agent_id is nil in @oganisation in create action.

params content

 id: nil,
 name: "ABC Org",
 organisation_size: "35",
 area: "USA"
 agent_id: nil,
 management: "DEMO"
user12763413
  • 1,177
  • 3
  • 18
  • 53

0 Answers0