2016-08-05 84 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 } 
相关问题