Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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"
like image 536
Jumbalaya Wanton Avatar asked Dec 05 '13 03:12

Jumbalaya Wanton


1 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.

like image 89
meagar Avatar answered Sep 19 '22 22:09

meagar