Matteo Matteo - 6 months ago 17
SQL Question

undefined method `order' for []:Array

I have the following code where I load the activities for the user based on whether or not they are an admin. Users can only see their own activity feed, whereas admins can see the entire stream. This is a precursor to sharing activity feeds with friends, etc.

def index
if current_user?
@incidents = Incident.find_all_by_user_id(current_user.id).order("created_at desc")
else
@incidents = Incident.all.order("created_at desc")
end
end


I am getting the above referenced error(
undefined method "order" for []:Array
). It seems to be the
.order
reference, but I have checked the rails Guides and it seems to be correct syntax.

Any ideas?

Answer

Try changing the find_by... to where, so:

def index
    if current_user?
      @incidents = Incident.where(user_id: current_user.id).order("created_at desc")
    else
      @incidents = Incident.all.order("created_at desc")
    end
  end

should work :-)

Comments