This is the continuation of a previous problem that I was able to solve: Parameters not saving properly (Rails)
But now I’ve encountered a stranger issue. Although I can get the parameter to save, it is unresponsive when I attempt to refer to it.
The Model:
# == 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
Object:
#<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">]
View:
<%= Message.find_by_id(60).content %>
<%= Message.find_by_id(60).recipient_list %>
The result is that the call on content returns as expected: “foobar123” However, the call on recipient_list returns only nil. Despite there clearly being a value there. I suspect that the recipient_list=(ids) method may be overriding the usual function of what the @message.recipient_list would otherwise be doing. Am I at least in the right ballpark? What’s going on here?
You redefined default
recipient_listmethod with your getter on this line:I think you have to remove it. Then everything should be fine.
I don’t know what does your comment mean, your model has getters and setters for all of the table columns.