为什么在Rails 5.0中未定义方法column_types?

blu*_*mmy 5 ruby rspec ruby-on-rails ruby-on-rails-5

我正在为一个类分配任务,它在rspec测试中使用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
Run Code Online (Sandbox Code Playgroud)

结束

错误:当我在命令行中运行rpsec时。
Rails 5.0
Ubuntu 14.10

失败/错误:期望(User.column_types [“ username”]。type).to eq:string

 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)>'
Run Code Online (Sandbox Code Playgroud)

lcg*_*ida 5

该方法已在此提交中删除。找到它不是那么容易。

但是,原因没有得到记录,这是因为该方法本身没有得到记录(也许仅供内部使用)。

如果存在该方法,请参见此 :nodoc:方法的注释

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

您可以通读提交的描述以了解原因,并查看是否还有其他可以做的事情。

编辑

看看这些线也许attributes_types还是columns_hash可以解决你的问题。


Ali*_*lan 5

column_typesRails 5 中删除了该方法。

要获取列的类型,您可以尝试以下代码:

User.column_for_attribute('username').type
Run Code Online (Sandbox Code Playgroud)

这将返回类型,在您的情况下: :string