I have 2 models:
class Mission < ActiveRecord::Base
belongs_to :category
end
class Category < ActiveRecord::Base
has_many :missions
end
And I have a complex Mission find statement:
@missions = Mission.send(@view, level).send(@show).search(@search).paginate :page => params[:page], :order => actual_sort, :per_page => 50
I'd like to add to my query the pos开发者_运维问答sibility to search for a specific category too. I tried this but it does not work:
@missions = Mission.send(@view, level).send(@show).send(:category, @category).search(@search).paginate :page => params[:page], :order => actual_sort, :per_page => 50
Rails says that Mission has not a .category method. How would you solve this?
Thanks, Augusto
OH ... MY ... GOD
Are you sure this is the best way to be doing this?
I don't think people will be able to help you if you don't explain a bit more, but I highly doubt that you couldn't write your statement like so:
@missions = Mission.select("missions.level ...,category.attr ...").where(["missions.level = ? ...", level ...]).includes(:category).where(["categories.field = ?", ...]).paginate(...)
Obviously the elipses (...) mean generally etc.
This is a working example on one of my projects in testing:
i = Item.where("items.name like '%Coupon%'").includes(:category).where(["categories.name = ? ",'Category 2'])
try performing the where selection on category_id
精彩评论