Rails ability to call my class method on an Array -


considering have following classes , records in db each one:

class user < activerecord::base   has_many :user_roles   has_many :roles, :through => :user_roles end  class role < activerecord::base   has_many :user_roles   has_many :users, :through => :user_roles    def self.method1     "boo!"   end end 

why user.first.roles.method1 work successfully? thought user.first.roles returns array, class doesn't have method1 defined.

> user.first.roles.method1 => "boo!" 

of course, role.all.method1 throws undefined method error expected.

my stack: ruby 1.9.3p484, rails 3.2.13

actually not array. activerecord::associations::collectionproxy can check definition here activerecord/lib/active_record/associations/collection_proxy.rb

it has changed method_missing sends requests parent class.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -