Skip to content

Commit

Permalink
use query_constraints instead of foreign_key in relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
zmariscal committed Feb 3, 2024
1 parent d48d10a commit 42c0c19
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ services:
context: .
environment:
DB_HOST: mysql
AR_VERSION: 7.0
AR_VERSION: 7.1
volumes:
- .:/usr/src/app
depends_on:
Expand Down
3 changes: 2 additions & 1 deletion test/models/book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

class Book < ActiveRecord::Base
belongs_to :topic, inverse_of: :books
belongs_to :tag, query_constraints: [:tag_id, :parent_id]
belongs_to :tag, foreign_key: [:tag_id, :parent_id] if ENV['AR_VERSION'].to_f < 7.1
belongs_to :tag, query_constraints: [:tag_id, :parent_id] if ENV['AR_VERSION'].to_f >= 7.0

has_many :chapters, inverse_of: :book
has_many :discounts, as: :discountable
Expand Down
7 changes: 6 additions & 1 deletion test/models/customer.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# frozen_string_literal: true

class Customer < ActiveRecord::Base
has_many :orders,
inverse_of: :customer,
foreign_key: [:account_id, :customer_id],
primary_key: %i(account_id id) if ENV['AR_VERSION'].to_f < 7.1

has_many :orders,
inverse_of: :customer,
query_constraints: [:account_id, :customer_id],
primary_key: %i(account_id id)
primary_key: %i(account_id id) if ENV['AR_VERSION'].to_f >= 7.0
end
7 changes: 6 additions & 1 deletion test/models/order.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# frozen_string_literal: true

class Order < ActiveRecord::Base
belongs_to :customer,
inverse_of: :orders,
foreign_key: [:account_id, :customer_id],
primary_key: %i(account_id id) if ENV['AR_VERSION'].to_f < 7.1

belongs_to :customer,
inverse_of: :orders,
query_constraints: [:account_id, :customer_id],
primary_key: %i(account_id id)
primary_key: %i(account_id id) if ENV['AR_VERSION'].to_f >= 7.0
end
3 changes: 2 additions & 1 deletion test/models/tag_alias.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

class TagAlias < ActiveRecord::Base
belongs_to :tag, query_constraints: [:tag_id, :parent_id], required: true
belongs_to :tag, foreign_key: [:tag_id, :parent_id], required: true if ENV['AR_VERSION'].to_f < 7.1
belongs_to :tag, query_constraints: [:tag_id, :parent_id], required: true if ENV['AR_VERSION'].to_f >= 7.0
end

0 comments on commit 42c0c19

Please sign in to comment.