I have a model like this:
class Lesson
include Mongoid::Document
field :title, :type => String
field :category, :type => String
field :price, :type => Float
field :description, :type => String
field :user_id, :type => String
validates_presence_of :title
validates_presence_of :category
validates_presence_of :price
validates_presence_of :user_id
attr_accessible :title, :category, :description, :price
end
And I am trying to query like this:
@lessons_by_user = Lesson.find_by_user_id current_user.id
And I am getting:
undefined method `find_by_user_id’ for Lesson:Class
How can I query by a specific attribute in MongoID?
I know how to do it like this:
@lessons = Lesson.all(:conditions=>{:user_id=>current_user.id.to_s})
but I am wondering if there is a shortcut…
Mongoid doesn’t have the ActiveRecord style auto-created finder methods, it only supports a limited set of predefined finder methods:
Model.allModel.countModel.exists?Model.findModel.find_or_create_byModel.find_or_initialize_byModel.firstModel.lastHowever, it does have a general purpose
wheremethod so you say this:whereis chainable as well (just likewherein newer versions of ActiveRecord) so you can add more conditions or specify the ordering by chaining more criteria calls.