Skip to content

Commit

Permalink
Associations should be defined before of being used in :through
Browse files Browse the repository at this point in the history
In Active Record 5.1+ isn't possible to use an association in `:through`
option before of defining it, e.g:

```
has_many :rubygems, through: :ownerships
has_many :ownerships, dependent: :destroy
```

will raise an error because the `ownerships` association is being used
in the first association before of being defined, to fix it the order of
the associations needs to be changed:

```
has_many :ownerships, dependent: :destroy
has_many :rubygems, through: :ownerships
```
  • Loading branch information
guilleiguaran committed May 1, 2018
1 parent 88ced40 commit 4f87c66
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
4 changes: 2 additions & 2 deletions app/models/rubygem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ class Rubygem < ApplicationRecord
include Patterns
include RubygemSearchable

has_many :owners, through: :ownerships, source: :user
has_many :ownerships, dependent: :destroy
has_many :subscribers, through: :subscriptions, source: :user
has_many :owners, through: :ownerships, source: :user
has_many :subscriptions, dependent: :destroy
has_many :subscribers, through: :subscriptions, source: :user
has_many :versions, dependent: :destroy, validate: false
has_one :latest_version, -> { where(latest: true).order(:position) }, class_name: "Version"
has_many :web_hooks, dependent: :destroy
Expand Down
5 changes: 3 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ class User < ApplicationRecord
].freeze

before_destroy :yank_gems

has_many :ownerships, dependent: :destroy
has_many :rubygems, through: :ownerships

has_many :subscriptions, dependent: :destroy
has_many :subscribed_gems, -> { order("name ASC") }, through: :subscriptions, source: :rubygem

has_many :deletions
has_many :ownerships, dependent: :destroy
has_many :subscriptions, dependent: :destroy
has_many :web_hooks, dependent: :destroy

after_validation :set_unconfirmed_email, if: :email_changed?, on: :update
Expand Down

0 comments on commit 4f87c66

Please sign in to comment.