I’m trying to create/update a location while saving a checkin in my Rails project, but am having trouble.
A Checkin belongs_to :location and, a Location has_many :checkins
I’m using accepts_nested_attributes_for :location, :allow_destroy => true in my checkin model, and am trying to create a location when creating a checkin like so:
POST "checkin[note]=this-is-great&checkin[user_id]=123&checkin[location_attributes][name]=popeyes&checkin[location_attributes][id]=314" to http://localhost:3000/checkins.json
However, everytime I run that, it throws an error saying
Couldn’t find Location with ID=314 for Checkin with ID=
I’m not sure what I’m doing right..? I’d like for it to create the Location with a specific ID if it doesn’t exist, and update that same location (according the location_id) if it does exist.
The location table has an id (primary key) and name (varchar)
The checkin table has an id (primary key, auto-increment) and note (varchar)
Anyone successfully work with accepts_nested_attributes_for?
This is what I did to solve the problem:
I changed the id of Locations from primary to primary+auto-increment, and stopped trying to set it manually. Instead of making it the same as the foursquare_id that I was trying to store, I decided to store the foursquare_id separately (in a different column).
My checkin model looks like this: