关联搜索逻辑

发布于 2024-09-16 00:53:05 字数 516 浏览 2 评论 0原文

我有以下关联:

class BookShelf {
  has_many :books
}

class Book {
  has_many :pages
  belongs_to :book_shelf
}

class Page {
  belongs_to :book
}

如果我有一个 BookShelf 对象,我如何使用 searchlogic 来搜索属于当前书架中书籍的页面?

目前我正在使用这个:

@bookshelf = BookShelf.find 1
@pages = Page.title_like("something").book_id_equals(@bookshelf.books.map { |book| book.id }).paginate :page => params[:page]

所以分页部分只是使用 will_paginate ,这并不是真正相关的。

关键是我认为这些代码行有点难看。有什么改进吗?

I have the following associations:

class BookShelf {
  has_many :books
}

class Book {
  has_many :pages
  belongs_to :book_shelf
}

class Page {
  belongs_to :book
}

If I have a BookShelf object, how could I use searchlogic to search pages that belong to the books in the current book shelf?

Currently I'm using this one:

@bookshelf = BookShelf.find 1
@pages = Page.title_like("something").book_id_equals(@bookshelf.books.map { |book| book.id }).paginate :page => params[:page]

So the paginate part is just the use of will_paginate which is not really relavent.

The point is I think these lines of codes are somewhat ugly. Is there any improvements?

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

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

发布评论

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

评论(1

尾戒 2024-09-23 00:53:05

这应该可以解决问题,并且无需将所有书籍 id 映射到数组中:

Page.title_like("something").book_bookshelf_id_equals(@bookshelf.id).paginate(:page => params[:page])

我在我的一个应用程序中测试了它,其中我使用以下命令有一个类似的模型结构(郊区 -> 地区 -> 州) :

Suburb.name_like("east").region_state_name_like("vi").paginate(:page => 3)

这似乎可以解决问题。

This should do the trick, and remove the need to map all the book ids into an array:

Page.title_like("something").book_bookshelf_id_equals(@bookshelf.id).paginate(:page => params[:page])

I tested it in one of my apps, where I had a similar model structure (Suburb -> Region -> State) using the following:

Suburb.name_like("east").region_state_name_like("vi").paginate(:page => 3)

Which seemed to do the trick.

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