0

我正在尝试同时做一个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%') 
4

2 回答 2

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]}%" ])
于 2013-10-09T20:43:27.060 回答
0

在 Ruby 中执行此操作:

@policies = Policy.find(:all,:conditions=>['state= 0 AND deleted = 0'])
count = @policies.length
于 2013-10-09T20:40:22.733 回答