You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is there a way to set parents as part of object creation? I'd like users to be able to pick the parents of an object in the standard Rails form for creating a new object.
The text was updated successfully, but these errors were encountered:
Their purpose seems to be to prevent entries in the links table with a null ancestor_id or descendant_id column. That's an admirable goal, but is running afoul of Rails filling those in automatically in this case after validation.
I.e. check for the associated object rather than the object's id. That's usually a better practice, but even that doesn't work in this case. At the point validations run both descendant and descendant_id are nil.
Note that if you take the validations out then this particular scenario works fine. Sometime after validation time Rails fills in descendant and everything ends up correct.
Hmm, some searching turns up rails/rails#6161 and rails/rails#8269 which suggest that adding :inverse_of to the associations will fix this. And indeed it does. OK, pull request coming soon.
Let's say you've got a Person model with has_dag_links and "attr_accessible :parents".
In the Rails console:
1.9.3p194 :001 > Person.create
=> #<Person id: 1, created_at: "2013-01-18 22:49:30", updated_at: "2013-01-18 22:49:30">
1.9.3p194 :002 > p=Person.new(parents: [Person.first])
1.9.3p194 :003 > p.valid?
1.9.3p194 :004 > p.errors.first
=> [:links_as_child, "is invalid"]
It works fine if you create a Person without parents, then add parents. E.g.
1.9.3p194 :001 > p=Person.create
=> #<Person id: 2, created_at: "2013-01-18 22:58:09", updated_at: "2013-01-18 22:58:09">
1.9.3p194 :002 > p.parents << Person.first
SQL (0.3ms) INSERT INTO "person_links" ("ancestor_id", "count", "descendant_id", "direct") VALUES (?, ?, ?, ?) [["ancestor_id", 1], ["count", 1], ["descendant_id", 2], ["direct", true]]
Is there a way to set parents as part of object creation? I'd like users to be able to pick the parents of an object in the standard Rails form for creating a new object.
The text was updated successfully, but these errors were encountered: