Rails:表列返回nil(无响应) [英] Rails: table column returning nil (unresponsive)

查看:92
本文介绍了Rails:表列返回nil(无响应)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我能够解决的先前问题的延续:参数未正确保存(铁轨)

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.

模型:

# == 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.尽管那里显然有价值.我怀疑receive_list =(ids)方法可能会覆盖@ message.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?

推荐答案

使用以下方法在您的getter上重新定义默认的recipient_list方法:

You redefined default recipient_list method with your getter on this line:

attr_reader :recipient_list #necessary for jquery-token-fields

我认为您必须将其删除.然后,一切都会好起来的.

I think you have to remove it. Then everything should be fine.

我不知道您的评论是什么意思,您的模型中所有表列都有getter和setter.

I don't know what does your comment mean, your model has getters and setters for all of the table columns.

这篇关于Rails:表列返回nil(无响应)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆