关联搜索逻辑
我有以下关联:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该可以解决问题,并且无需将所有书籍 id 映射到数组中:
我在我的一个应用程序中测试了它,其中我使用以下命令有一个类似的模型结构(郊区 -> 地区 -> 州) :
这似乎可以解决问题。
This should do the trick, and remove the need to map all the book ids into an array:
I tested it in one of my apps, where I had a similar model structure (Suburb -> Region -> State) using the following:
Which seemed to do the trick.