Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why the method column_types is undefined in Rails 5.0?

Im doing an assignment for a class and it uses column_types method in a rspec test.

  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

end

Error: when i run rpsec in the command line.
Rails 5.0
Ubuntu 14.10

Failure/Error: expect(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)>'
like image 522
bluejimmy Avatar asked Aug 05 '16 08:08

bluejimmy


3 Answers

The method was removed in this commit. It's not so easy to find it.

But, the reason is was not documented, it's because the method itself is not documented (Maybe it's just for internal use).

See this comment :nodoc: on the method when it existed:

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

You can read through the commit's description to understand the why and maybe see if there's something else you can do.

EDIT

Take a look at these lines maybe attributes_types or columns_hash can solve your problem.

like image 68
lcguida Avatar answered Oct 23 '22 19:10

lcguida


The method column_types is removed in Rails 5.

To get the type of column you can try following code:

User.column_for_attribute('username').type

This will return the type, here in your case: :string

like image 43
Ali Arslan Avatar answered Oct 23 '22 19:10

Ali Arslan


looks like in rails 5, column_types method no longer exist

like image 1
Tony Vincent Avatar answered Oct 23 '22 19:10

Tony Vincent