Activerecord has_many custom method


#1

Hi,
I’m trying to create this method:

  has_many :cars do
    Car.where(color: 'red').all
  end

I’m using a class without Activerecord and outside rails:

module API
class User < Base
include ActiveRecord::Associations

   has_many :cars do
    Car.where(color: 'red').all
  end
end

end

When I run:

undefined method `dangerous_attribute_method?’

How can I use a has_many and create my own implementation ?
Thanks.


#2

Have a look at rails scope. I’m pretty sure this is the functionality
you are looking for.
http://guides.rubyonrails.org/active_record_querying.html#scopes
http://guides.rubyonrails.org/active_record_querying.html#scopes

Basically, you define your association, then define a scope to retrieve
the records how you’d like to.