hyperrjas
hyperrjas

Reputation: 10744

ruby count for object attribute

I have a object with the next field:

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

In my controller in action repost, I want increase the counter + 1.

I try:

@post.repost += 1

but I get:

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

How can I fix it?.

Upvotes: 1

Views: 403

Answers (1)

Sandip Ransing
Sandip Ransing

Reputation: 7733

first initialize repost with default zero

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

@post.repost += 1

Upvotes: 4

Related Questions