Skip to content

Instantly share code, notes, and snippets.

@Incanus3
Created June 23, 2014 12:38
Show Gist options
  • Save Incanus3/3210403c996c5de2b297 to your computer and use it in GitHub Desktop.
Save Incanus3/3210403c996c5de2b297 to your computer and use it in GitHub Desktop.
ActiveRecord::AttributeMethods#column_for_attribute doesn't work with alias_attribute and behaves strangely with nonexistent attribute
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'arel', github: 'rails/arel'
gem 'sqlite3'
GEMFILE
system 'bundle'
end
require 'bundler'
Bundler.setup(:default)
require 'active_record'
require 'minitest/autorun'
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Schema.define do
create_table :posts do |t|
t.string :title
end
end
class Post < ActiveRecord::Base
alias_attribute :aliased_title, :title
end
class ColumnForAttributeTest < Minitest::Test
def setup
@post = Post.create! title: 'An awesome post'
@real_column = @post.column_for_attribute(:title)
end
def test_aliased_attribute
aliased_column = @post.column_for_attribute(:aliased_title)
p aliased_column.name #>> 'aliased_title' - there's no such column!
p aliased_column.type #>> nil!
assert_equal @real_column.name, aliased_column.name
assert_equal @real_column.type, aliased_column.type
end
def test_nonexistent_attribute
nonexistent_column = @post.column_for_attribute(:nonexistent)
p nonexistent_column.name #>> 'nonexistent' - again no such column!
p nonexistent_column.type #>> nil - seems reasonable
# or some other way to tell us there's a problem
assert_equal nil, nonexistent_column.name
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment