Rails 3 类方法与 select("...") 给出了错误数量的参数

发布于 2024-11-28 11:11:17 字数 1222 浏览 7 评论 0 原文

我希望使用这种方法

@results = Candidate.all.match_against(options)

,其中“选项”是整数数组。它应该向我返回所有候选人的比赛分数。

在候选模型中,我有一种方法可以将候选者与作为参数给出的选项进行匹配。

def self.match_against(cu_options)                                           
  select("candidates.id, candidates.first_name, candidates.last_name, candidates.number, 
  sum(case when c_answers.option_id in (?) then 1 else 0 end) as score", cu_options)
  .joins("join answers as c_answers on c_answers.user_id = candidates.user_id")
  .joins("join options on c_answers.option_id = options.id")
  .where("options.ordering >= 0")
  .group("candidates.id, candidates.number, candidates.first_name, candidates.last_name")
end

由于某种原因,当我调用此方法时,它给了我一个错误,

ArgumentError: wrong number of arguments (2 for 1)
from /home/mika/.rvm/gems/ruby-1.9.2-p180@vaalikone/gems/activerecord-3.0.7/lib/active_record/relation/query_methods.rb:34:in `select'
from /home/mika/.rvm/gems/ruby-1.9.2-p180@vaalikone/gems/activerecord-3.0.7/lib/acteve_record/base.rb:442:in `select'
from /home/mika/projects/vaalikone/app/models/candidate.rb:35:in `match_against'
...

我不知道第二个参数来自哪里。我只传递一个参数。我正在使用 postgresql。

I wish to use this method like

@results = Candidate.all.match_against(options)

, where "options" is an array of integers. And it should return me all candidates with their match score.

In Candidate model I have a method for matching candidates against options given as a parameter.

def self.match_against(cu_options)                                           
  select("candidates.id, candidates.first_name, candidates.last_name, candidates.number, 
  sum(case when c_answers.option_id in (?) then 1 else 0 end) as score", cu_options)
  .joins("join answers as c_answers on c_answers.user_id = candidates.user_id")
  .joins("join options on c_answers.option_id = options.id")
  .where("options.ordering >= 0")
  .group("candidates.id, candidates.number, candidates.first_name, candidates.last_name")
end

For some reason when I'm calling this method it gives me an error

ArgumentError: wrong number of arguments (2 for 1)
from /home/mika/.rvm/gems/ruby-1.9.2-p180@vaalikone/gems/activerecord-3.0.7/lib/active_record/relation/query_methods.rb:34:in `select'
from /home/mika/.rvm/gems/ruby-1.9.2-p180@vaalikone/gems/activerecord-3.0.7/lib/acteve_record/base.rb:442:in `select'
from /home/mika/projects/vaalikone/app/models/candidate.rb:35:in `match_against'
...

I have no idea where the second argument is coming from. I'm passing in only one argument. I'm using postgresql.

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

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

发布评论

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

评论(1

小ぇ时光︴ 2024-12-05 11:11:17

实际上,您将两个参数传递给选择范围:

  • 字符串“candidates.id,candidates.first_name,candidates.last_name,candidates.number,
    sum(case when c_answers.option_id in (?) then 1 else 0 end) as Score"
  • 变量 cu_options

这有什么原因吗?

Actually, you are passing two arguments to the select scope:

  • the string "candidates.id, candidates.first_name, candidates.last_name, candidates.number,
    sum(case when c_answers.option_id in (?) then 1 else 0 end) as score"
  • the variable cu_options

Is there any reason for that?

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