4

For a regular method I can just call obj.method(:name).source_location and that works great, however if I do that for associations the source_location points me to .../activerecord-4.2.0/lib/active_record/associations/builder/association.rb is there anything similar for associations?

I'm working with Spree with a bunch of extensions and there are way too many places where it can be defined, I can grep my way to it but wondering if there's a straight forward way like source_location

geermc4
  • 578
  • 6
  • 18
  • 1
    I'm pretty sure that association methods are dynamically generated, IOW, they don't *have* a source location, simply because they don't have "source code"! – Jörg W Mittag Sep 16 '15 at 18:18
  • What I was trying to shoot for was the file which defined the association, any extension in spree can add associations so wasn't like opening the model definition and looking at it :) – geermc4 Sep 16 '15 at 18:51

1 Answers1

1

There is a method reflect_on_association which might help you.

Customer.reflect_on_association(:accounts).class_name #=> Account
klass = 'Account'.constantize
klass.method(:my_method).source_location
dimakura
  • 7,575
  • 17
  • 36
  • not quite, the association is probably a `has_one` and is not defined in the other class, but thanks, helpful – geermc4 Sep 16 '15 at 17:52
  • 2
    @geermc4 if it's a has_one, then the singularized form of accounts should do, right? ;) – Ninigi Sep 16 '15 at 17:55
  • *probably* a has_one just guessing as I haven't found it, also, the associated class doesn't respond to the method – geermc4 Sep 16 '15 at 18:08
  • Doh, you guys are probably right, this one in particular was just a foreign key on the record /shy – geermc4 Sep 16 '15 at 18:30