Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save harmon/103429 to your computer and use it in GitHub Desktop.
Save harmon/103429 to your computer and use it in GitHub Desktop.
Something change in Rails 2.3.2 since 2.1.2, and I'm not sure what, or how to handle it.
Watch how the 'role' column (which the below shows that it is a private method, even though
it's just a column in the users table) gets handled by the different versions.
Rails 2.3.2 throws an error when first getting it, 2.1.2 doesn't.
Both update their classes to show that its not a private method after it has been set once!
In Rails 2.3.2:
=====================================
Loading development environment (Rails 2.3.2)
>> cols = User.columns.map {|c| c.name}
=> ["id", "name", "login", "email", "crypted_password", "salt", "created_at", "updated_at", "remember_token", "remember_token_expires_at", "role"]
>> cols.map {|c| User.private_instance_methods.include?(c)}
=> [false, false, false, false, false, false, false, false, false, false, true]
>> u = User.new
#<User id: nil, name: nil, login: nil, email: nil, crypted_password: nil, salt: nil, created_at: nil, updated_at: nil, remember_token: nil, remember_token_expires_at: nil, role: nil>
=> #<User id: nil, name: nil, login: nil, email: nil, crypted_password: nil, salt: nil, created_at: nil, updated_at: nil, remember_token: nil, remember_token_expires_at: nil, role: nil>
>> u.role
NoMethodError: Attempt to call private method
from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/attribute_methods.rb:236:in `method_missing'
from (irb):4
>> cols.map {|c| User.private_instance_methods.include?(c)}
=> [false, false, false, false, false, false, false, false, false, false, true]
>> u.role = 'hello'
=> "hello"
>> u.role
=> "hello"
>> cols.map {|c| User.private_instance_methods.include?(c)}
=> [false, false, false, false, false, false, false, false, false, false, false]
In Rails 2.1.2:
=====================================
Loading development environment (Rails 2.1.2)
>> cols = User.columns.map {|c| c.name}
=> ["id", "name", "login", "email", "crypted_password", "salt", "created_at", "updated_at", "remember_token", "remember_token_expires_at", "role"]
>> cols.map {|c| User.private_instance_methods.include?(c)}
=> [false, false, false, false, false, false, false, false, false, false, true]
>> u = User.new
#<User id: nil, name: nil, login: nil, email: nil, crypted_password: nil, salt: nil, created_at: nil, updated_at: nil, remember_token: nil, remember_token_expires_at: nil, role: nil>
=> #<User id: nil, name: nil, login: nil, email: nil, crypted_password: nil, salt: nil, created_at: nil, updated_at: nil, remember_token: nil, remember_token_expires_at: nil, role: nil>
>> u.role
=> nil
>> cols.map {|c| User.private_instance_methods.include?(c)}
=> [false, false, false, false, false, false, false, false, false, false, false]
>> u.role = 'hello'
=> "hello"
>> u.role
=> "hello"
>> cols.map {|c| User.private_instance_methods.include?(c)}
=> [false, false, false, false, false, false, false, false, false, false, false]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment