Rails: 在定义关联之前不能有 has_many :through 关联
Rails: Cannot have a has_many :through association before association is defined
正在将应用程序从 Rails 4.2.9 升级到 Rails 5.2.1。
通过许多令人讨厌的部分更新依赖项和诸如此类的东西,最终在 console
中有了应用程序 运行,现在试图在 server
上访问页面。部分页面加载但其他页面:
Cannot have a has_many :through association 'User#clubs' which goes through 'User#memberships' before the through association is defined.
不清楚 Rails 5 中可能发生了什么变化来触发这个?甚至不确定从哪里开始寻找。
想法?
下面调用的行似乎失败了:
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
这是从 model/story.rb
调用的
def self.viewable_published_stories_for(user)
ViewableStories
.for_user(user)
.includes(:cover_image, :user, :table_of_contents)
.published
.chronological
end
这可能只是您模型中的一个排序问题。 has_many
必须在 has_many through
之前。
所以现在,您可能有:
class User < ApplicationRecord
...
has_many :clubs, through: :memberships
has_many :memberships
...
end
您只需将 has_many :memberships
移动到 has_many through
上方:
class User < ApplicationRecord
...
has_many :memberships
has_many :clubs, through: :memberships
...
end
正在将应用程序从 Rails 4.2.9 升级到 Rails 5.2.1。
通过许多令人讨厌的部分更新依赖项和诸如此类的东西,最终在 console
中有了应用程序 运行,现在试图在 server
上访问页面。部分页面加载但其他页面:
Cannot have a has_many :through association 'User#clubs' which goes through 'User#memberships' before the through association is defined.
不清楚 Rails 5 中可能发生了什么变化来触发这个?甚至不确定从哪里开始寻找。
想法?
下面调用的行似乎失败了:
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
这是从 model/story.rb
def self.viewable_published_stories_for(user)
ViewableStories
.for_user(user)
.includes(:cover_image, :user, :table_of_contents)
.published
.chronological
end
这可能只是您模型中的一个排序问题。 has_many
必须在 has_many through
之前。
所以现在,您可能有:
class User < ApplicationRecord
...
has_many :clubs, through: :memberships
has_many :memberships
...
end
您只需将 has_many :memberships
移动到 has_many through
上方:
class User < ApplicationRecord
...
has_many :memberships
has_many :clubs, through: :memberships
...
end