3
我只是想知道是否有可能在Rails中「重命名」關聯。讓我們假設:Rails屬性別名
# An ActiveRecord Class named SomeModelASubModel (some_model_a_sub_model.rb)
class SomeModelASubModel < ActiveRecord::Base
has_many :some_model_a_sub_model_items
end
# An ActiveRecord Class named SomeModelASubModelItem (some_model_a_sub_model_item.rb)
class SomeModelASubModelItem < ActiveRecord::Base
belongs_to :some_model_a_sub_model
end
在這一點上,呼籲some_model.items,其中some_model是SomeModelASubModel類的實例會觸發一個未定義的方法錯誤。
儘管如此,最佳做法是什麼? :
# With a method_alias or something, would it be possible to :
some_model = SomeModelASubModel.first # for instance
items = some_model.items
# For the reason stated, this doesn't work, one has to call :
items = some_model.some_model_a_sub_model_items
這樣的簡寫可能嗎?
預先感謝您!
感謝您的快速回復,那正是我所需要的。只是不知道該怎麼做:) – Dr1Ku 2010-05-15 14:15:17