Why can't I save an object with a serialized attribute?

▼魔方 西西 提交于 2019-12-24 14:09:05

问题


I have...

/app/models/search.rb:

serialize :result  
def multisearch
  self.result = PgSearch.multisearch(self.term)
  self.status = "closed"
  self.save
  return result
end

/db/schema.rb:

create_table "searches", :force => true do |t|
  t.string   "term"
  t.string   "status"
  t.text     "result"
end

I get the following error when I try `self.save?

ArgumentError: wrong number of arguments (2 for 1)
from /Users/steven/.rvm/gems/ruby-1.9.2-p320/gems/arel-3.0.2/lib/arel/expressions.rb:3:in `count'

I get a similar error when I test result.serialize:

ArgumentError: wrong number of arguments (0 for 1)
from /Users/steven/.rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.11/lib/active_record/attribute_methods/serialization.rb:49:in `serialize'

How can I fix this?


回答1:


Answer was to convert to an array before serialization: self.result = PgSearch.multisearch(self.term).to_a



来源:https://stackoverflow.com/questions/15671773/why-cant-i-save-an-object-with-a-serialized-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!