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

ActiveRecord Where query with parent attribute? #232

Closed
zw963 opened this issue May 28, 2015 · 3 comments
Closed

ActiveRecord Where query with parent attribute? #232

zw963 opened this issue May 28, 2015 · 3 comments

Comments

@zw963
Copy link

zw963 commented May 28, 2015

milk = Category.find_by(name: 'milk')

Category.create(name: 'acid milk', parent: milk)   # => this worked for me

Category.where(name: 'acid milk', parent: milk)  # => this not worked, not implement yet ??
# PG::UndefinedColumn: ERROR:  column categories.parent does not exist
@zw963
Copy link
Author

zw963 commented May 28, 2015

Have a more quickly method to do this? (add a field ??)

What I does is:

Category.where(name: 'acid milk').find {|x| x.parent == milk }

@vanderhoorn
Copy link
Contributor

Ancestry only uses the ancestry column. i.e. parent_id is not an ActiveRecord column. Instead, parent and parent_id are methods, so you cannot use parent in a where-clause.

What about:

milk = Category.find_by(name: 'milk')

milk.children.where(name: 'acid milk')

@zw963
Copy link
Author

zw963 commented May 28, 2015

Thanks, your's answer more efficient than me in my case.

@zw963 zw963 closed this as completed May 28, 2015
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

No branches or pull requests

2 participants