2013-03-06 100 views
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 
+0

未定義的方法「拆分」的第二個錯誤是因爲Test.where(:test_id => test_id)返回一個Arel節點而不是單個單獨的測試。您可能想要使用Test.find_by_test_id(test_id)。 – Rebitzele 2013-03-06 21:33:31

+1

謝謝......至少,讓我繼續深入下一個錯誤,哈哈) – 2013-03-06 22:02:34

回答

1

有一種可能性,即單詞「測試「以某種方式由導軌保留。

我的模型的屬性名稱之一是「test」。當我試圖撥打instance.update_attributes(:test => SOMEVALUE)時,它會返回相同的錯誤的參數數量(0代表2..3)''您收到的消息。

當我將屬性的名稱從「test」更改爲其他名稱時,錯誤消失。

相關問題