简体   繁体   English

Searchlogic关于关联

[英]Searchlogic on associations

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? 如果我有BookShelf对象,如何使用searchlogic搜索属于当前书架中书籍的页面?

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. 所以分页部分只是使用will_paginate,实际上并没有涉及。

The point is I think these lines of codes are somewhat ugly. 关键是我认为这些代码行有些丑陋。 Is there any improvements? 有什么改进吗?

This should do the trick, and remove the need to map all the book ids into an array: 这应该可以解决问题,并且不需要将所有书籍ID映射到数组中:

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. 这似乎可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM