Pregunta

I'm trying to create a condition in my condition to check if a policy has an insurance

My tables:

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

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

This is what I'm trying to do

If policy has an insurance do
  @condition
else
  nothing
end

Here is my model:

class Policy < ActiveRecord::Base
  has_many :insurances
end

class Insurance < ActiveRecord::Base
  belongs_to :policy
end     

Here is my controller:

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

Please somebody can help me with this?

I will really appreciate help

¿Fue útil?

Solución

I guess you are trying to find if a particular Policy has an associated insurance.

What about doing this:

if @policy.insurances.size > 0
 # that policy have insurances
else
 # that policy does not have insurances
end
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top