简体   繁体   English

has_many通过关联和多个搜索条件,我该如何进行这项工作?

[英]has_many through association, and multiple search criteria, how can i make this work?

I'm trying to create a multi criteria search form. 我正在尝试创建一个多条件搜索表单。 I want to submit all of the pieces of the search via GET and if they have a value assigned I would like them evaluated. 我想通过GET提交所有搜索内容,如果它们分配了值,我希望对它们进行评估。 The thing that I'm having trouble grasping is building a query that will allow me to layer more queries on top when you're doing it with a through association. 我遇到的麻烦是要构建一个查询,当您使用“通过”关联进行查询时,它将允许我在顶部放置更多查询。

Just to give you an idea of how my models are set up: 只是让您了解我的模型是如何设置的:

class Client < ActiveRecord::Base
  has_many :campaigns
  has_many :pieces, :through => :campaigns
end

class Campaign < ActiveRecord::Base
  belongs_to :client
  has_many :pieces
end

class Piece < ActiveRecord::Base
  belongs_to :client
end

Now, with that model in mind, I'm using the collect method to grab the pieces that have an organization in common. 现在,考虑到该模型,我正在使用collect方法来抓取具有共同组织的部分。

if params.has_key?(:criteria)
    @selected_client = Client.where(:organization => "Org1")
    @pieces = @selected_client.collect{ |c| c.pieces }.flatten
end

Is there some way of formatting that query string so that I can narrow @pieces down, a couple more times? 有什么方法可以格式化该查询字符串,以便我可以将@pieces缩小两次。 Let's say I wanted to use that through association again, to get pieces that have another of the same Client criteria... 假设我想通过关联再次使用它,以获得具有其他相同客户标准的作品...

Thanks a ton! 万分感谢! My brain is a pretzel at this point. 此时我的大脑是椒盐脆饼。

I'm not sure if i undestand very well what you're trying to do. 我不确定我是否很好理解您的意图。 If you want to get all pieces matching your client criteria, in rails 3, you can do this : 如果您想获得所有符合客户标准的作品,请在rails 3中执行以下操作:

@pieces = Piece.joins(:campaign => :client).where(:clients => {:organization => criteria})

to get all pieces belonging to clients from organization "Org1". 从组织“ Org1”获取属于客户的所有作品。

You can stack as many where statements as you want to add new conditions, as @Andrew said. 如@Andrew所说,您可以堆叠要添加新条件的where语句。 See this for more information . 请参阅此以获得更多信息

Your first paragraph got cut off, just FYI. 您的第一段被截断,仅供参考。

If @pieces is an array, you can use a find block to narrow your search further. 如果@pieces是数组,则可以使用find块进一步缩小搜索范围。 Although this will put the load on your server CPU rather than the SQL database. 尽管这会将负载放在服务器CPU而非SQL数据库上。

You can stack where statements and Rails will automatically create a single query for you. 您可以where语句和Rails会自动为您创建单个查询的where堆叠。 Here is some sample code from the app store section of our website: 以下是我们网站的“应用商店”部分中的一些示例代码:

@platform = params[:platform]
@category = params[:category]
@search   = params[:app_q]

# Get initial SQL, narrow the search, and finally paginate
@apps = App.live

@apps = @apps.where("platform = ?", AppPlatforms.value(@platform)) if AppPlatforms.value(@platform)
@apps = @apps.where("(name LIKE ? OR description LIKE ?)", "%#{@search}%", "%#{@search}%") if @search
@apps = @apps.where("id IN(?)", @cat.apps) if @category && @cat = Category.find_by_id(@category)

@apps = @apps.paginate(:page => params[:page], :per_page => 10, :order => "name")

Instead of using collect you should be able to use @selected_client.pieces.where(...) to narrow your search down via SQL. 您应该可以使用@selected_client.pieces.where(...)而不是使用collect来通过SQL缩小搜索范围。

I hope this is a point in the right direction! 我希望这是朝正确方向迈出的一步!

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

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