Chaining the hell out of ActiveRecord models

So, you want a quick and dirty way to obtain all the models in your Rails app that contain relations targeting a collection (like an has_many relation)?

Dir['app/models/**/*.rb']
  .map {|model| File.basename(model, '.rb')}
  .map(&:camelize)
  .map(&:constantize)
  .select {|section|
    section.reflections.find {|name, reflection|
      reflection.collection?
    }
  }.map(&:to_s)

Demeter law?

You got served

kind of.. ^_^

Leave a Reply

Please Login to comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.