3
我有一個Rails 2.3應用程序與以下模型。Rails:任何方式預加載(包括)父協會
class Message << AR::Base
has_many :message_copies
end
class MessageCopy << AR::Base
belongs_to :message
end
每當我查詢MessageCopy,我總是需要引用父消息的屬性。所以我總是會預加載(通過:include =>:message)來減少db查詢的數量。
到目前爲止,我想出了這一點:
named_scope :with_parent_msg, :include => :message
這讓我很容易地做到這一點:
@user.message_copies.with_parent_msg
有沒有更好的方式來做到這一點?所以我不必一直使用_parent_msg調用?
歡迎任何建議。謝謝!
我假設的語法在某個時刻改變,但此爲Rails 5.x的正確的語法:** default_scope {包括(:消息)} * * – 2016-09-28 18:31:29