- Notifications
You must be signed in to change notification settings - Fork 22k
Add test case to test enum in has_many #33661
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
Add test case to test enum in has_many #33661
Conversation
| Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @eileencodes (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review. Please see the contribution instructions for more information. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason that enum works is that SpecialAuthor inherites from Author and has_many :books is defined in the Author. This assertion should pass with defined byclass SpecialAuthor < ActiveRecord::Base.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, thx
I can make it RED now, using
class SpecialAuthor < ActiveRecord::Base self.table_name = "authors" has_many :my_books, class_name: "SpecialBook", foreign_key: :author_id end class SpecialBook < ActiveRecord::Base self.table_name = "books" belongs_to :author enum read_status: { unread: 0, reading: 2, read: 3, forgotten: nil } end still finding whether/how to fix it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rails/activerecord/lib/active_record/relation/predicate_builder.rb
Lines 69 to 70 in e2d8a2c
| if value.is_a?(Hash) && !table.has_column?(key) | |
| associated_predicate_builder(key).expand_from_hash(value) |
I think it is because associated_predicate_builder("books") does not refer to SpecialBook, while associated_predicate_builder("my_books") does
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can think of two way to fix it
- keep using
SpecialAuthor.joins(:my_books).where(books: { read_status: "reading" })
Expect:
SELECT "authors".* FROM "authors" INNER JOIN "books" ON "books"."author_id" = "authors"."id" WHERE "books"."read_status" = 2 Actual:
SELECT "authors".* FROM "authors" INNER JOIN "books" ON "books"."author_id" = "authors"."id" WHERE "books"."read_status" = 'reading' Rails need to use table_name books go through all its associations to find out my_books, there maybe more associations using table book
- add feature to support
SpecialAuthor.joins(:my_books).where(my_books: { read_status: "reading" })
Expect:
SELECT "authors".* FROM "authors" INNER JOIN "books" ON "books"."author_id" = "authors"."id" WHERE "books"."read_status" = 2 Actual:
SELECT "authors".* FROM "authors" INNER JOIN "books" ON "books"."author_id" = "authors"."id" WHERE "my_books"."read_status" = 2 Rails need to use association my_books to find out table_name books and use it in sql
I would prefer way 2.
@kamipo what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kamipo thanks for pointing that out.
for 6a6c4c4
SpecialAuthor.joins(:books, :my_books) will generate
SELECT "authors".* FROM "authors" INNER JOIN "books" ON "books"."author_id" = "authors"."id" INNER JOIN "books" "my_books_authors" ON "my_books_authors"."author_id" = "authors"."id" and where(books: { read_status: "reading" }) generates
WHERE "my_books"."read_status" = 2 is it possible to let where knows it should be my_books_authors from joins?
WHERE "my_books_authors"."read_status" = 2 There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It isn't possible at this point, since alias tracking for joins is lazy evaluated until build_arel is called.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kamipo thanks for the explanation. shall I update this PR to include new test cases for normal has_many :books only?
since there is no test to cover enum in has_many
There is test in has_one to test enum, but there is no for has_many. [Rich Chen]
da6f61b to eed8b23 Compare
There is test in has_one to test enum, but there is no for has_many.