English 中文(简体)
我怎么能写出最有效率的范围,只问某些儿童物体的父母。
原标题:How can I write the most efficient scope for asking only those parent objects which have some certain child objects

我有:

class Evaluation < ActiveRecord::Base
  has_many :scores
end
class Score < ActiveRecord::Base
  scope :for_disability_and_score,
        lambda { |disability, score|
          where( total_score >= ? AND name = ? , score, disability)
        }
end

<代码>分 表格有<条码>总数: 外地和<条码>:外地。

How can i write a scope to ask for only those evaluations which have a Score with name vision and total_score of 2 and they have another Score with name hearing and total_score of 3. And how can all this be generalized to ask for those evaluations which have n scores with my parameters?

在原材料中,它希望:

I managed to do it in raw sql:

sql = %q-SELECT "evaluations".*
FROM "evaluations"
INNER JOIN "scores" AS s1 ON "s1"."evaluation_id" = "evaluations"."id"
INNER JOIN "target_disabilities" AS t1 ON "t1"."id" = "s1"."target_disability_id"
INNER JOIN "scores" AS s2 ON "s2"."evaluation_id" = "evaluations"."id"
INNER JOIN "target_disabilities" AS t2 ON "t2"."id" = "s2"."target_disability_id"
WHERE "t1"."name" =  vision  AND (s1.total_score >= 1)
      AND "t2"."name" =  hearing  AND (s2.total_score >= 2)-

这里要重申这一点:

INNER JOIN "scores" AS s1 ON "s1"."evaluation_id" = "evaluations"."id"

(通过将第1条改为第2和第3条等等)

WHERE (s1.total_score >= 1)

但是,它应当成为这样做的铁路...... 希望

问题回答

引证:

scope :by_scores, lambda do |params|
    if params.is_a? Hash
      query = joins(:scores)
      params.each_pair do |name, score|
        query = query.where(  scores.total_score >= ? AND scores.name = ? , score, name)
      end
      query
    end
 end

接着呼吁:

Evaluation.by_scores  vision  => 2,  hearing  => 3




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签