Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Person < ActiveRecord::Base
- def name=(name)
- super(name.capitalize)
- end
- def name
- super().downcase # not sure why you'd do this; this is just an example
- end
- end
- def name=(name)
- write_attribute(:name, name.capitalize)
- end
- def name
- read_attribute(:name).downcase # No test for nil?
- end
- def name=(name)
- self[:name] = name.capitalize
- end
- def name
- self[:name].downcase
- end
- class Post < ActiveRecord::Base
- serialize :foo
- def foo
- read_attribute(:foo)
- end
- end
- ./script/plugin install git://github.com/chriseppstein/has_overrides.git
- class Post < ActiveRecord::Base
- has_overrides
- module Overrides
- # put your getter and setter overrides in this module.
- def title=(t)
- super(t.titleize)
- end
- end
- end
- $ ./script/console
- Loading development environment (Rails 2.3.2)
- >> post = Post.new(:title => "a simple title")
- => #<Post id: nil, title: "A Simple Title", body: nil, created_at: nil, updated_at: nil>
- >> post.title = "another simple title"
- => "another simple title"
- >> post.title
- => "Another Simple Title"
- >> post.update_attributes(:title => "updated title")
- => true
- >> post.title
- => "Updated Title"
- >> post.update_attribute(:title, "singly updated title")
- => true
- >> post.title
- => "Singly Updated Title"
Add Comment
Please, Sign In to add comment