I have two related models such as this:
class PartCategory < ActiveRecord::Base
has_many :part_types
scope :engine, where(:name => 'Engine')
end
class PartType < ActiveRecord::Base
belongs_to :part_category
end
I would like to add a scope to the PartType model such as:
scope :engine_parts, lambda { joins(:part_category).engine }
But when I try that, I get the following error:
NoMethodError: undefined method `default_scoped?' for ActiveRecord::Base:Class
I don't have a lot of experience with the scope thing, so I am probably missing something fundamental here. Can someone please tell me what it is.
Try this:
scope :engine_parts, lambda { joins(:part_category).merge(PartCategory.engine) }
Basically, the result of joins(:part_category)
is the join of two models, so you can't call .engine
on it directly, you need to compose scopes in this manner.
See Here for more
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With