2
我想重新將我作爲參數接收到的字符串重構爲單詞數組(使用split
方法)。我的Test
模型有一個屬性,名爲source
。錯誤的參數數量(0代表2..3)
class Test < ActiveRecord::Base
attr_accessible :source
serialize :sources, Array
after_create do
test.source.split(' ')
end
end
這將返回一個錯誤:
wrong number of arguments (0 for 2..3)
我想不出論據的Rails想要什麼。
UPD
如果我改變了這樣的代碼:
class Test < ActiveRecord::Base
attr_accessible :source
serialize :sources, Array
def split_this_text(test_id)
@test = Test.where(:test_id=>test_id)
@test.source.split(' ')
end
end
,並呼籲在tests_controller方法/製作:
@test.split_this_text(:id)
比我得到這個錯誤:
NoMethodError in TestsController#create
undefined method `split' for #<Arel::Nodes::JoinSource:0x4ddfc60>
UPD#2
最後作品的作品沒有任何錯誤,但行爲像沒有任何工程和源usuall字符串(例如@test.source[0]
返回一個字母)
class Test < ActiveRecord::Base
attr_accessible :source
serialize :sources, Array
before_save :split_this_text
def split_this_text
self.source.split(' ')
end
end
未定義的方法「拆分」的第二個錯誤是因爲Test.where(:test_id => test_id)返回一個Arel節點而不是單個單獨的測試。您可能想要使用Test.find_by_test_id(test_id)。 – Rebitzele 2013-03-06 21:33:31
謝謝......至少,讓我繼續深入下一個錯誤,哈哈) – 2013-03-06 22:02:34