My use case is simple:
Get all ads that have a category name of params[:category_name].
Here are my model associations:
class Ad < ActiveRecord::Base
belongs_to :category
attr_accessible :anonymous, :celular, :name, :precio, :category_id
end
class Category < ActiveRecord::Base
has_many :ads
attr_accessible :name
end
Here is my controller code:
def by_category
@ads = Ad.where(:category => params[:category_name])
end
But it’s not working as intended and I get an exception:
SQLite3::SQLException: no such column: ads.category: SELECT “ads”.* FROM “ads” WHERE “ads”.”category” = ‘aeronautica’
How can I filter the ad objects by category name?
In your ads database you only have a column called category_id, so searching the ads database for a category name has no use.
This would show all ads for a category with the category_name that is in the params.