这是我之前能够解决的问题的延续:参数未正确保存(Rails)
但是现在我遇到了一个奇怪的问题。虽然我可以获取要保存的参数,但当我尝试引用它时它没有响应。
该模型:
# == Schema Information
#
# Table name: messages
#
# id :integer not null, primary key
# content :text
# sender_id :integer
# recipient_list :text
# created_at :datetime not null
# updated_at :datetime not null
#
class Message < ActiveRecord::Base
attr_accessible :content, :sender_id, :recipient_list
attr_reader :recipient_list #necessary for jquery-token-fields
serialize :recipient_list, Array
validates :content, presence: true
validates :sender_id, presence: true
def recipient_list=(ids) #necessary for jquery-token-fields
recipient_list = ids.split(",")
super(recipient_list)
end
end
目的:
#<Message id: 60, content: "foobar123", sender_id: 1, recipient_list: ["1", "2"], created_at: "2012-08-23 06:40:00", updated_at: "2012-08-23 06:40:00">]
看法:
<%= Message.find_by_id(60).content %>
<%= Message.find_by_id(60).recipient_list %>
结果是对内容的调用按预期返回:“foobar123” 但是,对接收者列表的调用仅返回 nil。尽管那里显然存在价值。我怀疑 recipient_list=(ids) 方法可能会覆盖 @message.recipient_list 否则会执行的常规功能。我至少在正确的球场上吗?这里发生了什么?