使 ActiveRecord 连接模型属性在查询结果中可用

发布于 2025-01-06 07:12:17 字数 1433 浏览 0 评论 0原文

给定 User 和 Book 模型,我创建了一个连接模型 ViewedBook,其中包含其他属性。以下是我提出的要点:

create_table "users" 
    t.string   "username"
end

create_table "books" 
    t.string   "title"
    t.integer "user_id"
    t.date "authored_date"
end

create_table "books_viewings"
    t.integer  "book_id"
    t.integer  "user_id"
    t.boolean "finished"
    t.date "last_viewed_date"
end

class User 
    belongs_to :book_viewing

    has_many    :authored_books,
                :class_name => "Book",
                :source => :book

    has_many :book_viewings

    has_many    :viewed_books :through => :book_viewings
                :order => "book_viewings.last_viewed_date DESC"

    has_many    :finished_books :through => :book_viewings
                :conditions => "book_viewings.finished = TRUE",
                :order => "book_viewings.last_viewed_date DESC"
end

class Book
    belongs_to :user
    has_one :author, :class_name => "User"
end

class BookViewing
    belongs_to :book
    belongs_to :user
end

我认为这适用于我需要创建的大多数查询。但是,我希望 user.viewed_books 返回的每个 Book 对象也包含 finished 属性。此外,我还将有其他查询,例如 Book.best_sellers,我还希望将其范围限定到给定用户,以便它们也包含已完成的属性。

从我对 ActiveRecord 的有限了解来看,似乎有一种优雅的方法来管理它并生成有效的查询,但我还没有找到一个示例来阐明这种情况。

编辑:澄清一下,我正在寻找的其他查询本身并不局限于已完成的书籍,但我需要将 finished 属性附加到每个查询book_viewings 中给定书籍和范围用户是否存在该书籍。

Given User and Book models, I've created a join model, ViewedBook, that contains additional attributes. Below is the essence of what I've come up with:

create_table "users" 
    t.string   "username"
end

create_table "books" 
    t.string   "title"
    t.integer "user_id"
    t.date "authored_date"
end

create_table "books_viewings"
    t.integer  "book_id"
    t.integer  "user_id"
    t.boolean "finished"
    t.date "last_viewed_date"
end

class User 
    belongs_to :book_viewing

    has_many    :authored_books,
                :class_name => "Book",
                :source => :book

    has_many :book_viewings

    has_many    :viewed_books :through => :book_viewings
                :order => "book_viewings.last_viewed_date DESC"

    has_many    :finished_books :through => :book_viewings
                :conditions => "book_viewings.finished = TRUE",
                :order => "book_viewings.last_viewed_date DESC"
end

class Book
    belongs_to :user
    has_one :author, :class_name => "User"
end

class BookViewing
    belongs_to :book
    belongs_to :user
end

I think this works for most of the queries I need to create. However, I want each of the Book objects returned by user.viewed_books to include the finished attribute as well. Further, I will have additional queries like Book.best_sellers that I would also like to scope to a given user so that they also include the finished attribute.

From my limited exposure to ActiveRecord, it appears there's probably an elegant way to manage this and generate efficient queries, but I have yet to find an example that clarifies this scenario.

EDIT: to clarify, the other queries I'm looking for will not themselves be restricted to books that have been finished, but I need to have the finished attribute appended to each book if it exists for the given book and scoped user in book_viewings.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

So尛奶瓶 2025-01-13 07:12:17

在这里查看我的答案 https://stackoverflow.com/a/8874831/365865

几乎,不,没有具体方法是执行此操作,但您可以将选择选项传递给 has_many 关联。对于你的情况我会这样做:

has_many :books, :through => :book_viewings, :select => 'books.*, book_viewings.finished as finished'

See my answer here https://stackoverflow.com/a/8874831/365865

Pretty much, no there isn't a specific way to do this, but you can pass a select option to your has_many association. In your case I'd do this:

has_many :books, :through => :book_viewings, :select => 'books.*, book_viewings.finished as finished'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文