Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ambiguous column name: error not working with tags

I have some code to display my recipes in order of their popularity as shown below. This is stored in my index file for the recipe view.

<% @all_recipes.this_week.reorder("likes_count DESC").limit(10).each do |i| %>
  <%=link_to image_tag(i.image(:fixed), alt: "MUC", :class => "browse-recipes"), i %>
<% end %>

As you can see I have passed a method 'this_week' which works perfectly defined as a scope.

My issue is, I have a tag cloud on the same page and when I click on a tag is should refresh the page but showing only those recipes with the associated tag.This works perfectly WITHOUT the this_week method applied but if the method is there I get the following error:

SQLite3::SQLException: ambiguous column name: created_at: SELECT  "recipes".* FROM     "recipes" JOIN taggings recipes_taggings_81e277c  ON recipes_taggings_81e277c.taggable_id = recipes.id AND recipes_taggings_81e277c.taggable_type = 'Recipe' AND recipes_taggings_81e277c.tag_id = 33 WHERE "recipes"."live" = 't' AND (created_at > '2014-01-13 00:00:00.000000')  ORDER BY likes_count DESC LIMIT 10

Any thoughts? Much appreciated.

like image 969
MikeHolford Avatar asked Jan 17 '14 16:01

MikeHolford


1 Answers

You must have a created_at field in both the recipes and tags tables. MySQL doesn't know which one it should order by. Where ever you are doing the ordering (in the this_week scope or in the tag method) you need to change this:

order('created_at')

to this:

order('recipes.created_at')

Assuming you want to order by the recipe's field. And if you don't like hardcoding the table name you can use self.class.table_name and substitute that in.

like image 94
Philip Hallstrom Avatar answered Oct 18 '22 00:10

Philip Hallstrom