Created
May 3, 2010 20:13
-
-
Save rbarazi/388527 to your computer and use it in GitHub Desktop.
[Beginning Rails 3] Listing 7-28. Updated app/models/article.rb
This file contains 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 Article < ActiveRecord::Base | |
validates :title, :presence => true | |
validates :body, :presence => true | |
belongs_to :user | |
has_and_belongs_to_many :categories | |
has_many :comments | |
scope :published, where("articles.published_at IS NOT NULL") | |
scope :draft, where("articles.published_at IS NULL") | |
scope :recent, lambda { published.where("articles.published_at > ?", 1.week.ago.to_date)} | |
scope :where_title, lambda { |term| where("articles.title LIKE ?", "%#{term}%") } | |
def long_title | |
"#{title} - #{published_at}" | |
end | |
def published? | |
published_at.present? | |
end | |
def owned_by?(owner) | |
return false unless owner.is_a? User | |
user == owner | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment