通过关系表连接 Rails 中的表
我有三门课;用户、提要和条目。用户有一个或多个提要,并且提要有一个或多个条目:
class Feed < ActiveRecord::Base
has_many :entries
has_many :feeds_users
has_many :users, :through => :feeds_users, :class_name => "User"
end
Class User < ActiveRecord::Base
has_many :feeds_users
has_many :feeds, :through => :feeds_users, :class_name => "Feed"
end
class Entry < ActiveRecord:Base
belongs_to :feed
end
用户和提要类通过第三个表相关:
class FeedsUser < ActiveRecord::Base
belongs_to :user
belongs_to :feed
end
现在,我的问题是我需要获取某个用户的提要以及每个提要的最新 n 个条目。我正在摆弄这样的事情:
u = User.first
Feed.joins(:entries, :users).where(:entries => { :created_at => (Time.now-2.days)..Time.now }, :users => u)
我的问题是,这会生成一些试图查询表“feeds”上的“users”的SQL - 该表不存在。
如何创建一个正确的查询来为我提供给定用户的所有提要及其最新 10 个条目?
I have three classes; User, Feed, and Entries. Users have one or more Feeds and Feeds have one or more Entries:
class Feed < ActiveRecord::Base
has_many :entries
has_many :feeds_users
has_many :users, :through => :feeds_users, :class_name => "User"
end
Class User < ActiveRecord::Base
has_many :feeds_users
has_many :feeds, :through => :feeds_users, :class_name => "Feed"
end
class Entry < ActiveRecord:Base
belongs_to :feed
end
The User and Feed classes are related through a third table:
class FeedsUser < ActiveRecord::Base
belongs_to :user
belongs_to :feed
end
Now, my problem is that I need to get a certain user's feeds and each feed's latest n entries. I'm fiddling with something like this:
u = User.first
Feed.joins(:entries, :users).where(:entries => { :created_at => (Time.now-2.days)..Time.now }, :users => u)
My problem here is that this generates some SQL that tries to query "users" on the table "feeds" - which doesn't exist.
How do I go about creating a correct query that can give me all feeds and their latest 10 entries for a given user?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
取出
has_many :feeds_users
。:feeds_users
不应该是模型。您应该保留该表,但 Rails 的一些魔力仍在幕后进行。如果您有一个名为 feeds 的表和一个名为 users 的表,Rails 将派生查找的表名称,除非您以不同方式指定它。
对于您的示例:
它将在
users_feeds
表中查找关系。如果它看起来像这样:它将在
user_feeds
表中查找关系。它在语言上是正确的,因为在第一个示例中许多用户将有许多提要,而在第二个示例中一个用户将有许多提要。这也是你的关系逻辑是否正确的一个很好的指标。
听起来您还需要
has_and_belongs_to_many
关系,而不仅仅是has_many
。有关所有形式的模型关联的完整概述,请查看指南。
Take out
has_many :feeds_users
.:feeds_users
shouldn't be a model.You should keep the table, but some Rails magic goes on behind the scenes. If you have a table named feeds and a table named users, Rails derives the table name of the look-up unless you specify it differently.
For your example:
It will look for the relationship in the
users_feeds
table. If it looked like this:It would look for the relationship in the
user_feeds
table.It's linguistically correct, as many users will have many feeds in the first example, and one user will have many feeds in the second example. That's also a good indicator of whether your relation logic is correct.
It also sounds like you need the
has_and_belongs_to_many
relationship instead of justhas_many
.For a complete run-down on all forms of model associations, check out the guide.