Skip to content

Conversation

@jychen7
Copy link
Contributor

@jychen7 jychen7 commented Aug 20, 2018

There is test in has_one to test enum, but there is no for has_many.

@rails-bot
Copy link

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.

Copy link
Member

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.

Copy link
Contributor Author

@jychen7 jychen7 Aug 24, 2018

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

Copy link
Contributor Author

@jychen7 jychen7 Aug 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor Author

@jychen7 jychen7 Aug 25, 2018

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

  1. 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

  1. 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?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

way 1 is #30787.

way 2 had been applied but had already been reverted at 6a6c4c4 to fix the regression #20308.

Copy link
Contributor Author

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 
Copy link
Member

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.

Copy link
Contributor Author

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]
@jychen7 jychen7 force-pushed the 33428-test-has-many-association-enum branch from da6f61b to eed8b23 Compare August 25, 2018 12:13
@kamipo kamipo merged commit 519d09c into rails:master Aug 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

4 participants