Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: Cannot have a has_many :through association before association is defined

Upgrading an app from Rails 4.2.9 to Rails 5.2.1.

Through much of the nasty part updating dependencies & whatnot and finally have app running in the console and now trying to hit pages on server. Some pages load but others:

Cannot have a has_many :through association 'User#clubs' which goes through 'User#memberships' before the through association is defined.

Not clear what may have changed in Rails 5 to trigger this? Not even sure where to start looking.

Thoughts?

Seems to fail on line called out below:

class ViewableStories
  ...
  def for_user
    Story
      .includes(:publications)
      .references(:publications)
      .where(
        stories[:user_id]
        .eq(@user.id)
        .or(
          publications[:club_id]
          .in(@user.club_ids)        <<==== execution halts
          .and(
            publications[:publish_on]
            .lt(Date.today)
            .or(publications[:publish_on].eq(nil))
          )
        )
      )
  end
end

Which is called from model/story.rb

  def self.viewable_published_stories_for(user)
    ViewableStories
      .for_user(user)
      .includes(:cover_image, :user, :table_of_contents)
      .published
      .chronological
  end
like image 368
Meltemi Avatar asked Oct 30 '18 23:10

Meltemi


People also ask

What are rails associations and how to use them?

I’ll go over some keywords that you might have seen and tried to use and give examples on how to effectively use Rails Associations. Associations are basically defining the relationship between models. When speaking of models, we should go under the hood and understand what the code you are writing does.

What does as as mean in rails?

Setting the :as option indicates that this is a polymorphic association, as discussed earlier in this guide. If you set the :autosave option to true, Rails will save any loaded association members and destroy members that are marked for destruction whenever you save the parent object.

Why is belongs_to not working in rails?

This is because Rails automatically infers the class name from the association name. If the association name is wrongly pluralized, then the inferred class will be wrongly pluralized too. When used alone, belongs_to produces a one-directional one-to-one connection.

What is a “through” association?

In our example, that would be the association representing a user having many followers or followed_users. However, we have explored this to one more level, ie. a has_many :through association where the “through” attribute indirectly references the same model (User) as the source.


1 Answers

It is probably just an ordering issue in your model. The has_many has to come before the has_many through.

So right now, you probably have:

class User < ApplicationRecord
  ...
  has_many :clubs, through: :memberships
  has_many :memberships
  ...
end

You just need to move the has_many :memberships above the has_many through:

class User < ApplicationRecord
  ...
  has_many :memberships
  has_many :clubs, through: :memberships
  ...
end
like image 161
AbM Avatar answered Sep 29 '22 22:09

AbM