0

我正在尝试在我的条件中创建一个条件来检查保单是否有保险

我的桌子:

|policies|
  |id|    |num_policy|  
   1        1234          
   2        5678           
   3        9123          
   4        4567           

|insurances|
  |id|  |policy_id|     |net_ensurance|
   1       1                1000

这就是我想要做的

If policy has an insurance do
  @condition
else
  nothing
end

这是我的模型:

class Policy < ActiveRecord::Base
  has_many :insurances
end

class Insurance < ActiveRecord::Base
  belongs_to :policy
end     

这是我的控制器:

  if Policy.has_insurance?
   @search = Policy.find(:all,:conditions =>['deleted = 0'])
  else
   @search= "nothing"
  end

请问有人可以帮我吗?

我将非常感谢帮助

4

1 回答 1

1

我猜您正在尝试查找特定保单是否有相关保险。

这样做怎么办:

if @policy.insurances.size > 0
 # that policy have insurances
else
 # that policy does not have insurances
end
于 2013-11-11T22:05:47.537 回答