Sunday, May 6, 2012

[Rails] Re: run script after initialize

Jeremy Walker wrote in post #1059748:
> On 6 May 2012, at 20:22, Guillem Vidal <lists@ruby-forum.com> wrote:
>
>> ActiveRecord::Base.send(:descendants) and
>> ActiveRecord::Base.send(:subclasses) is allways empty [].
>>
>> I've tried it on config/environment.rb and config/application.rb with no
>> success, can someone tell me what i can do about it?
>>
>
> You could just reopen ActiveRecord::Base. Create a new initializer (a
> ruby file in config/initializers) with:
>
> class ActiveRecord::Base
> def new_method
> # do stuff
> end
> end
>
> Does that help?
> Jeremy Walker
> http://www.ihid.co.uk

Not in my case. What I want to do is define different method names
depending on its relation, for example:

ActiveRecord::Base.send(:descendants).each do |subclass|
subclass.reflect_on_all_associations.each do |association|
macro = association.macro #:has_one, :has_many...
subclass.send(:define_method, "#{association.name}_tokens") do
read_attribute(__method__[0..-8])
end
end
end

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate