This project is archived and is in readonly mode.
nested attributes with belongs_to only supports one use case
-
Matt Jones
Not sure if there's a bug here - case 3 in your example can be done by dropping / removing the hidden id field, and I'm not sure what you're looking for in case 2. Normally, you'd handle cases like #2 rake rails:freeze:edge on windows by having a select attached to project_id (and/or nifty autocompleter stuff as needed).
-
Lyle
I thought nested attributes did more for me than it does. If anything, can the documentation be updated to specifically state what happens when you submit the form? If an ID field is included, that record will be renamed (which affects all records that related to it). If an ID field is not included, a new record is created regardless if one exists already (so you may end up with duplicates).
I think a better example of this is a task that has one color:
class Task < ActiveRecord::Base belongs_to :color accepts_nested_attributes_for :color endIn the form, if color is a text_field and the user changes the name from "red" to "green", the Color record with name="red" becomes name="green". If Color includes an RGB attribute then after this update, "green" will have RGB(255,0,0) -- or RGB red.
If possible, the documentation should include a way to override this behavior -- if that's possible. So, if a developer can include a method in their model, like def color_attributes, to do their own update of the attributes.
-
Daniel Heath
I don't think this was the intended purpose of accepts_nested_attributes_for.
The idea is to allow you to edit associated record values or add new associated records.If Task belongs_to Project, it only makes sense to use accepts_nested_attributes_for if you want to be able to modify the Project when editing the Task.
The documentation could be clearer. You should submit a patch (it's not hard to learn how, and contributing can be very rewarding).
The docs could mention that using an existing primary key value in the attributes hash indicates you are referencing an existing record (and that otherwise a new one will be created).
-
Ryan Bigg
- State changed from new to open
Please submit a patch if the documentation is still confusing.
-
Neeraj Singh
- State changed from open to invalid
- Importance changed from to Low
Anyone can update documentation at http://github.com/lifo/docrails .