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

wpDiscuz