我有一个 Conversation ActiveRecord 模型,基本上如下
class Conversation < ActiveRecord::Base
has_many :messages, :order => 'created_at ASC'
belongs_to :latest_message, :class_name => 'Message'
end
我希望将 latest_message 属性始终设置为 messages.last。我已经尝试过 before_save 回调,但是当我添加一条消息时,它似乎并没有被触发
conversation.messages.create!(
sender: user,
body: message_body
)
有任何想法吗?