Skip to content

Instantly share code, notes, and snippets.

@rickhull
Created October 4, 2010 01:52
Show Gist options
  • Select an option

  • Save rickhull/609143 to your computer and use it in GitHub Desktop.

Select an option

Save rickhull/609143 to your computer and use it in GitHub Desktop.
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
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