Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying to set parents as part of object creation triggers validation failure of [:links_as_child, "is invalid"] #11

Closed
jheiss opened this issue Jan 18, 2013 · 3 comments · Fixed by #17

Comments

@jheiss
Copy link

jheiss commented Jan 18, 2013

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.

@resgraph
Copy link
Owner

Hmmm. Let me think on this a bit. I will get back to you shortly.

@eturino
Copy link

eturino commented Oct 14, 2014

this is still an issue... @resgraph any updates?

@jheiss
Copy link
Author

jheiss commented Nov 19, 2014

The offending bit are these validations in lib/dag/dag.rb. The second one specifically is what results in the error I originally reported.

validates ancestor_id_column_name.to_sym, :presence => true,
          :numericality => true
validates descendant_id_column_name.to_sym, :presence => true,
          :numericality => true

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 tried simplifying them to this:

validates :ancestor, :presence => true
validates :descendant, :presence => true

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants