2016-08-05 85 views
4

我正在做一個類的任務,它使用rspec測試中的column_types方法。爲什麼Rails 5.0中的方法column_types未定義?

it "User database structure in place" do 
     expect(User.column_names).to include "password_digest", "username" 
     expect(User.column_types["username"].type).to eq :string 
     expect(User.column_types["password_digest"].type).to eq :string 
     expect(User.column_types["created_at"].type).to eq :datetime 
     expect(User.column_types["updated_at"].type).to eq :datetime 

錯誤:當我在命令行運行rpsec。
滑軌5.0
Ubuntu的14.10

故障/錯誤:預期(User.column_types [ 「用戶名」]類型)。爲了當量:串

NoMethodError: 
    undefined method `column_types' for #<Class:0x000000053a0188> 
    Did you mean? columns 
        column_names 
# ./spec/assignment_spec.rb:67:in `block (5 levels) in <top (required)>' 
# ./spec/assignment_spec.rb:14:in `block (2 levels) in <top (required)>' 
+0

hmm it被刪除 –

+0

@ArupRakshit'class User bluejimmy

回答

2

該方法在this commit中刪除。找到它並不容易。

但是,原因是沒有記錄,這是因爲方法本身沒有記錄(也許它只是爲了內部使用)。

this comment:nodoc:的方法時,它的存在:

def column_types # :nodoc: 
    @column_types ||= columns_hash.transform_values(&:cast_type).tap do |h| 
     h.default = Type::Value.new 
    end 
    end 

您可以通過commit's說明看明白了,爲什麼,也許看看是否有別的東西,你可以做。

編輯

看看these lines也許attributes_typescolumns_hash可以解決你的問題。

1

看起來像在導軌5,column_types方法沒有更長時間存在

+0

一個數字如何顯示?我用Google搜索了一下,並沒有對此做任何說明。是否有網站檢查方法和棄用? – bluejimmy

+0

是的,沒有機構提到它http://edgeguides.rubyonrails.org/5_0_release_notes.html#active-record-removals here .. -_- –

3

的方法column_types用Rails除去5

爲了讓你可以試試下面的代碼列的類型:

User.column_for_attribute('username').type 

這會在你的情況下返回類型,在這裏::string

0

你可以用它來得到所有column_types的散列

User.columns_hash.each_with_object({}) { |obj, h| h[obj[1].name] = obj[1].sql_type } 
相關問題