0

Basically, I have two models - Company and Location.

class Company < ActiveRecord::Base
  has_many :locations, dependent: :destroy
  accepts_nested_attributes_for :locations # Not sure if needed.
end

class Location < ActiveRecord::Base
  belongs_to :company
end

When creating a Company (i.e. a POST to /companies), I want to be able to create its Locations in the same request. But for some reason, I cannot get Rails to recognize the array of Locations nested inside the Company. It seems to rip the nested JSON array out and place it into the "root" of the JSON request.

Example POST request using cURL:

curl -X POST -H "Accept: Application/json" -H "Content-Type: application/json" http://example.com/companies -d '{"employee_count":320,"locations":[{"lat":"-47.5", "lon":"120.3"},{"lat":"78.27", "lon":"101.09"}]}'

Example server output:

Started POST "/companies"
Processing by CompaniesController#create as Application/json
Parameters: {"employee_count"=>320, "locations"=>[{"lat"=>"-47.5", "lon"=>"120.3"}, {"lat"=>"78.27", "lon"=>"101.09"}], "company"=>{"employee_count"=>320}}

As you can see, the locations array is no longer inside the company object, so when CompaniesController#create attempts create the Company instance, it's array of Locations is nil. Also, the employee_count attribute is repeated twice, which is kind of strange too. Does anyone know why is this happening and how to fix it?

For reference, this is what the whitelist in my CompaniesController looks like:

params.require(:company).permit(
  :employee_count,
  locations: [:lat, :lon]
)

My server is Thin (1.6.2), and it's pretty much an entirely new/default Rails app with no special configurations.

4
  • 1
    Check this:stackoverflow.com/questions/845366/… It may help You! Commented Apr 7, 2014 at 6:49
  • 3
    Nested attributes in strong parameters should be called like this: locations_attributes (both plural). Commented Apr 7, 2014 at 6:55
  • 1
    @rails4guides.com Exactly! Commented Apr 7, 2014 at 6:58
  • You'll have to show us your form & new / create controller actions -- the problem you have is you're not passing the nested attributes correctly (you have to build ActiveRecord objects when you send the request etc). I can write some help if you show us your controller actions & form Commented Apr 7, 2014 at 8:16

1 Answer 1

1

Okay, looks like I needed to rename locations to locations_updates in my POST request (thanks to the users in the comments who suggested this).

I also needed to encapsulate all attributes in an "outer" company object, instead of being at the "root" of the JSON. Example:

curl -X POST -H "Accept: Application/json" -H "Content-Type: application/json" http://example.com/companies -d '{"company":{"employee_count":320,"locations":[{"lat":"-47.5", "lon":"120.3"},{"lat":"78.27", "lon":"101.09"}]}}'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.