mercredi 24 juin 2015

how to check object is exits or not in view ROR

I have implemented query on distant lat long and create virtual field in query like this

user        = User.all
if longitude.present? && latitude.present? && fulladdress.present?

   distantQuery = "(3959 * acos( cos( radians("+latitude+") ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians("+longitude+")) + sin( radians("+latitude +")) * sin(radians(latitude))))  < " + miles
   user = User.select("*, (3959 * acos(cos(radians("+latitude+")) * cos( radians(latitude)) * cos(radians(longitude) - radians("+longitude+")) + sin(radians(" +latitude +")) * sin(radians(latitude))))  as distant").where(distantQuery)
end

when my if condition run then distant is coming other wise it will not coming

<% @result.each do |r| %>
<tr>
<td>
 <%= r.distant.present %>

</td>
</tr>

now in view my distant is come form query then it will show result and when it will not come then it show me error

undefined method `distant' for #<User:0x000000092a6558>

I have implement "present" in view

  <% if r.distant.present? %>
                        <%= r.distant.to_i %>   


                <% end %>

but still it show me error, What will i use to check if distant is coming then it will show and other wise not ?

Aucun commentaire:

Enregistrer un commentaire