Mirror318
Mirror318

Reputation: 12673

How to access a class with specific parent

I have a project with class Matrix < ActiveRecord::Base, and I'm hitting situations where Matrix.where is crashing because it's trying to access the Matrix < Enumerable class from ruby.

Is there a way to specify which Matrix I want, like ActiveRecord::Base::Matrix?

I know I could rename the model, but it would be a pain (old spaghetti project)

Upvotes: 1

Views: 36

Answers (1)

tadman
tadman

Reputation: 211570

Rename it or be forever battling Ruby core. You can't get around this. Rails chose to put model names in the root namespace as it's very convenient, but leads to problems when that conflicts with either Rails or Ruby.

Solutions:

  • Try and force-load your model before the Ruby Matrix has a chance to usurp it
    • This may have unintended consequences (like a crash) should require 'matrix' show up somewhere
    • This could also mess up gems that expect Matrix to be the Ruby thing and not your random model
  • Move it into a namespace like MyProject::Matrix
  • Rename it to something dumb like MMatrix as a reminder that this name was a bad idea from the outset

The good news is that if this is a fairly unique string, then a global replace of Matrix to the alternate name is probably not going to be that bad.

Upvotes: 1

Related Questions