What could cause a rails app to have a no method error when using a scope?
I have a basic user class that has cats and dogs. I will need to combine the queries and sort them by created date. Eventually the individual queries will be more complex.
class User < ActiveRecord::Base
has_many :dogs
has_many :cats
scope :pets, joins(:dogs).joins(:cats).order("created_at desc")
In view
<%= render @user.pets%>
Is causing an no method error
undefined method `pets' for #<User:0x00000106370cb0>
Scopes only define class methods on the ActiveRecord model. The proper way to call this would be on the User model directly.
User.petsas opposed to an instance of User@user.pets.What you could do is create a method to be called on a User instance.
And thus,
@user.petsis allowed.