ruby-on-rails - 对象属性的 ruby 计数

标签 ruby-on-rails ruby count

我有一个带有下一个字段的对象:

class Post
 field :repost , type: Integer
 attr_accessible :repost
end

在我重新发布 Action 的 Controller 中,我想增加计数器 + 1。

我尝试:

@post.repost += 1

但我得到:

NoMethodError (You have a nil object when you didn't expect it!
You might have expected an instance of Array.

我该如何解决?

最佳答案

首先使用默认零初始化repost

field :repost , type: Integer, :default => 0

@post.repost += 1

关于ruby-on-rails - 对象属性的 ruby 计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9160333/

相关文章:

ruby-on-rails - Rails 3.1 和 Coffeescript : Require entire directory

css - 忽略 Rails 默认布局文件

ruby - 如何调试sidekiq前端?

sql - PostgreSQL 从一个表中选择全部并从表关系中加入计数

sql - 根据单一标准从两个表中计数

ruby-on-rails - Rails 迁移 - self.up 和 self.down 有什么意义

ruby-on-rails - Rails 多选默认值

ruby - 如何使用 Nokogiri 将两个 XML 文件合并为一个文件?

ruby - Net::HTTP 请求连接太慢?

XSLT count() 不起作用