将应用程序从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
关于ruby-on-rails - Rails : Cannot have a has_many :through association before association is defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53074430/