Definition: class_name with a string or character in Rails 3

If I have a model Invitationand a model User, with a model Invitationhaving a column requester_id, why does this work,

class Invitation < ActiveRecord::Base
  belongs_to :requester, :class_name => "User"
end

and it doesn’t work?

class Invitation < ActiveRecord::Base
  belongs_to :requester, :class_name => :user
end

In general, when do I use characters and when do I use strings in Rails 3?

+3
source share
2 answers

Or one

You will need :User, not:user.

The Ruby core Symboldefines #to_sand Stringdefines #to_sym,, so in any Ruby context you can often replace one with another.

: , Rails3 API, , , . , .

+2

, . :User . , , :)

0

All Articles