Forum: Ruby on Rails Activerecord has_many custom method

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2d6b09a142da991a00d9d328ce46990a?d=identicon&s=25 Guilherme Mello (guilherme)
on 2016-05-18 15:07
(Received via mailing list)
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.
1e4bc344d2e910ab8bbaf3fa37838e55?d=identicon&s=25 BuyzLots (Guest)
on 2016-05-18 17:26
(Received via mailing list)
Have a look at rails scope.  I’m pretty sure this is the functionality
you are looking for.
http://guides.rubyonrails.org/active_record_queryi...
<http://guides.rubyonrails.org/active_record_queryi...

Basically, you define your association, then define a scope to retrieve
the records how you’d like to.
This topic is locked and can not be replied to.