0

Apologies for the long wined title, in my app I am trying to retrive e the last record from the database that matches the params I have passed to it.

@goals = Weight.last(:is_goal=>true,:user_id=>@user.id)

In my views I want to run a conditional that checks if there are any present and if it has it will display a div.

<% if @goals.any? %>
    <% @goals.each do |goal| %>
        <p><%= goal.amount %></p>
    <% end %>
<% end %>

But for some reason this throws a no method error NoMethodError at / undefined method 'any?'. If I change the .last to .all it works

@goals = Weight.all(:is_goal=>true,:user_id=>@user.id)

Is there any reason behind this or have I found a bug?

Sam Mason
  • 1,037
  • 1
  • 8
  • 29

1 Answers1

1

Well, .last method returns an object, .all method returns an array of objects.
And .any? is an array method. You can't call .any? on an object, it will tell you that there is no method unless you have created one.

MurifoX
  • 14,991
  • 3
  • 36
  • 60
  • Ah thats brilliant thanks for that, so is there an equivalent of `.any?` for just an object? – Sam Mason Mar 25 '13 at 18:35
  • 1
    You can try using `nil?`. As if there is no object, then it is set to nil. Keep in mind that it is not the same thing, it is just a different approach. – MurifoX Mar 25 '13 at 18:36