If I have a Invitation
model and a User
model, with the Invitation
model having a requester_id
column, why does this work,
class Invitation < ActiveRecord::Base
belongs_to :requester, :class_name => "Us开发者_JS百科er"
end
and this doesn't work?
class Invitation < ActiveRecord::Base
belongs_to :requester, :class_name => :user
end
In general, when do I use symbols, and when do I use strings in Rails 3?
Either One
You would certainly need :User
, not :user.
The core Ruby Symbol
defines a #to_s
and String
defines a #to_sym,
so in any Ruby context you may often substitute one for another.
I might be tempted to say: when in doubt, follow the Rails3 API documentation. except it doesn't say, although the example uses a string. But in this case, it works, you can safely use a symbol.
Remember that it is case sensitive. So :User
may work. In any case, I would use a string as that's what it says in the docs :)
精彩评论