我怎样才能同时计算和选择?

时间:2013-10-09 19:30:58

标签: ruby-on-rails ruby ruby-on-rails-2

我正在尝试同时进行selectcount

这是我的代码:

@count = Policy.find(:all,:conditions=>['state= 0 AND deleted = 0']

####i want to count all the policies  that has 1 client by document

@count =   Policy.count(:joins => :client,:conditions => ['doc_jur LIKE ? OR doc_nat LIKE ?', "%#{params[:doc]}%","%#{params[:doc]}%" ])

如何只在一行(一个动作)中执行此操作?

我想:

 SELECT count(*) AS count_all FROM `policies` 
 where state= 0 AND deleted = 0
 INNER JOIN `clients` ON `clients`.id = `policies`.client_id 
 WHERE (doc_jur LIKE '%20535920746%' OR doc_nat LIKE '%20535920746%') 

2 个答案:

答案 0 :(得分:0)

在Ruby中执行:

@policies = Policy.find(:all,:conditions=>['state= 0 AND deleted = 0'])
count = @policies.length

答案 1 :(得分:0)

试试这个:

  @count =Policy.count(:joins => :client,:conditions => ['policies.state=0 AND policies.deleted= 0 AND clients.doc_jur LIKE ? OR clients.doc_nat LIKE ?', "%#{params[:doc]}%","%#{params[:doc]}%" ])