Created
October 4, 2010 01:52
-
-
Save rickhull/609143 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class NewsItem < ActiveRecord::Base | |
| validates_presence_of(:date, :headline, :body) | |
| def date | |
| @date.strftime("%Y-%m-%d") if @date | |
| end | |
| def date=(input) | |
| case input | |
| when String | |
| puts "date is a string" | |
| @date = Date.parse(input) | |
| when Date | |
| puts "date is a date" | |
| @date = input | |
| when NilClass | |
| puts "date is nil" | |
| @date = nil | |
| else | |
| raise("Unexpected date: #{input}") | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| rails console | |
| Loading development environment (Rails 3.0.0.rc2) | |
| ruby-1.9.2-p0 > n = NewsItem.new | |
| => #<NewsItem id: nil, headline: nil, byline: nil, body: nil, created_at: nil, updated_at: nil, date: nil> | |
| ruby-1.9.2-p0 > n.date = '2008-10-20' | |
| date is a string | |
| => "2008-10-20" | |
| ruby-1.9.2-p0 > n.date | |
| => "2008-10-20" | |
| ruby-1.9.2-p0 > n | |
| => #<NewsItem id: nil, headline: nil, byline: nil, body: nil, created_at: nil, updated_at: nil, date: nil> | |
| ruby-1.9.2-p0 > | |
| # why is date stil nil? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment