-
Notifications
You must be signed in to change notification settings - Fork 21.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support has_many through associations with composite query_constraints
Given an association like: ```ruby BlogPost.has_many :blog_post_tags, query_constraints: [:blog_id, :blog_post_id] BlogPost.has_many :tags, through: :blog_post_tags ``` The `tags` association records can be queried and the resulting JOIN query will include both `blog_id` and `blog_post_id` in the ON clause.
- Loading branch information
1 parent
e6087bd
commit 5e915f3
Showing
9 changed files
with
150 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
_fixture: | ||
model_class: Sharded::BlogPostTag | ||
|
||
short_read_first_post_blog_one: | ||
tag_id: <%= ActiveRecord::FixtureSet.identify(:short_read_blog_one) %> | ||
blog_post_id: <%= ActiveRecord::FixtureSet.identify(:great_post_blog_one) %> | ||
blog_id: <%= ActiveRecord::FixtureSet.identify(:sharded_blog_one) %> | ||
|
||
technical_content_first_post_blog_one: | ||
tag_id: <%= ActiveRecord::FixtureSet.identify(:technical_blog_one) %> | ||
blog_post_id: <%= ActiveRecord::FixtureSet.identify(:great_post_blog_one) %> | ||
blog_id: <%= ActiveRecord::FixtureSet.identify(:sharded_blog_one) %> | ||
|
||
short_read_second_post_blog_one: | ||
tag_id: <%= ActiveRecord::FixtureSet.identify(:short_read_blog_one) %> | ||
blog_post_id: <%= ActiveRecord::FixtureSet.identify(:second_post_blog_one) %> | ||
blog_id: <%= ActiveRecord::FixtureSet.identify(:sharded_blog_one) %> | ||
|
||
beginner_content_first_post_blog_two: | ||
tag_id: <%= ActiveRecord::FixtureSet.identify(:beginner_blog_two) %> | ||
blog_post_id: <%= ActiveRecord::FixtureSet.identify(:great_post_blog_two) %> | ||
blog_id: <%= ActiveRecord::FixtureSet.identify(:sharded_blog_two) %> | ||
|
||
short_read_first_post_blog_two: | ||
tag_id: <%= ActiveRecord::FixtureSet.identify(:short_read_blog_two) %> | ||
blog_post_id: <%= ActiveRecord::FixtureSet.identify(:great_post_blog_two) %> | ||
blog_id: <%= ActiveRecord::FixtureSet.identify(:sharded_blog_two) %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
_fixture: | ||
model_class: Sharded::Tag | ||
|
||
short_read_blog_one: | ||
name: "short read" | ||
blog_id: <%= ActiveRecord::FixtureSet.identify(:sharded_blog_one) %> | ||
|
||
long_read_blog_one: | ||
name: "long read" | ||
blog_id: <%= ActiveRecord::FixtureSet.identify(:sharded_blog_one) %> | ||
|
||
technical_blog_one: | ||
name: "tech" | ||
blog_id: <%= ActiveRecord::FixtureSet.identify(:sharded_blog_one) %> | ||
|
||
time_management_blog_one: | ||
name: "time management" | ||
blog_id: <%= ActiveRecord::FixtureSet.identify(:sharded_blog_one) %> | ||
|
||
beginner_blog_two: | ||
name: "for beginners" | ||
blog_id: <%= ActiveRecord::FixtureSet.identify(:sharded_blog_two) %> | ||
|
||
intermediate_blog_two: | ||
name: "for intermediate" | ||
blog_id: <%= ActiveRecord::FixtureSet.identify(:sharded_blog_two) %> | ||
|
||
short_read_blog_two: | ||
name: "short read" | ||
blog_id: <%= ActiveRecord::FixtureSet.identify(:sharded_blog_two) %> | ||
|
||
breaking_news_blog_2: | ||
name: "breaking news" | ||
blog_id: <%= ActiveRecord::FixtureSet.identify(:sharded_blog_two) %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "sharded/blog" | ||
require_relative "sharded/blog_post" | ||
require_relative "sharded/comment" | ||
require_relative "sharded/tag" | ||
require_relative "sharded/blog_post_tag" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# frozen_string_literal: true | ||
|
||
module Sharded | ||
class BlogPostTag < ActiveRecord::Base | ||
self.table_name = :sharded_blog_posts_tags | ||
|
||
belongs_to :blog_post, query_constraints: [:blog_id, :blog_post_id] | ||
belongs_to :tag, query_constraints: [:blog_id, :tag_id] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
module Sharded | ||
class Tag < ActiveRecord::Base | ||
self.table_name = :sharded_tags | ||
query_constraints :blog_id, :id | ||
|
||
has_many :blog_post_tags, query_constraints: [:blog_id, :tag_id] | ||
has_many :blog_posts, through: :blog_post_tags | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters