ActiveRecord: Pass class instead of string to class_name when defining associations

Viewed 1325

Are there any implications or gotchas to passing in a class instead of a string when defining an association?

belongs_to :owner, class_name: User

As opposed to:

belongs_to :owner, class_name: "User"
2 Answers

The class may not be loaded yet in which case you'll get a NameError: uninitialized constant User.

You're supposed to use "User" for this reason, as implied by the option name: :class_name, not :class.

Related