我正在尝试使用搜索参数中的选择结果
SELECT distinct policy_business_unit_id as number FROM `policies` WHERE (policy_business_unit_id ='2')
该条件显示
number
2
我试图在我的参数条件下使用“2”。
SELECT distinct policy_business_unit_id as number FROM `policies` WHERE (policy_business_unit_id ='2')
SELECT * FROM `cia_ensures` WHERE (id = 2)
但我得到了这个
SELECT * FROM `cia_ensures` WHERE (id = NULL)
我的桌子:
|cia_ensures|
|id| |name|
1 aaa
2 bbb
3 ccc
...
|policy_business_units|
|id| |name|
1 abc
2 def
3 ghi
...
|policies|
|id| |number| |policy_business_unit_id|
1 1234 2
2 5678 1
3 9123 2
4 4567 1
...
这是控制器:
def ajax
@business = PolicyBusinessUnit.find(:all)
@select = Policy.find(:all,:select=>['distinct policy_business_unit_id as number'],:conditions=>['policy_business_unit_id = ?',params[:business] ])
@list_cias = CiaEnsure.find(:all,:conditions=>['id = ?',@select])
end
以下是模型:
class Policy < ActiveRecord::Base
belongs_to :cia_ensure
end
class CiaEnsure < ActiveRecord::Base
has_many :policies
end
以下是观点:
Business:
<%= select_tag "business",options_for_select(@list_cias.collect {|t| [t.name.to_s ,t.id]}, params[:business].to_i ) %>
Cias:
<%= select_tag "cias",options_for_select(@business.collect {|t| [t.name.to_s ,t.id]}, params[:cias].to_i ) %>
我要做的是:
@business = PolicyBusinessUnit.find(:all)
@select = Policy.find(:all,:select=>['distinct policy_business_unit_id as number'],:conditions=>['policy_business_unit_id = ?',params[:business] ])
@list_cias = CiaEnsure.find(:all,:conditions=>['id = ?',@select[0].try(:number) ])
这是我的日志:
##### HERE I SHOULD HAVE THE NUMBER 2
SELECT distinct policy_business_unit_id FROM `policies` WHERE (policy_business_unit_id ='2')
##### I WANT TO USE NUMBER 2
SELECT * FROM `cia_ensures` WHERE (id = NULL)
请有人帮助我吗?
答案 0 :(得分:1)
当你提供
@list_cias = CiaEnsure.find(:all,:conditions=>['id = ?',@select[0].try(:number) ])
您确定@select[0]
包含响应:number
的对象吗?
您可以向我们提供@select
变量吗?
PS:如果您使用的是比rails 2.3
更新的任何内容,请重新阅读AR查询