Created
March 14, 2010 03:04
-
-
Save lxneng/331740 to your computer and use it in GitHub Desktop.
has_many
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
create_table "messages", :force => true do |t| | |
t.string "subject" | |
t.text "body" | |
t.datetime "created_at" | |
t.datetime "updated_at" | |
t.string "message_type" | |
t.integer "user_id" | |
t.boolean "deleted", :default => false | |
end | |
class User < ActiveRecord::Base | |
has_many :messages | |
end | |
class Messages < ActiveRecord::Base | |
MESSAGE_TYPES = %w[Recipient Author] | |
belongs_to :user | |
named_scope :recipient, :conditions => {:message_type => 'Recipient'} | |
named_scope :author, :conditions => {:message_type => 'Author'} | |
named_scope :deleted, :conditions => {:deleted => true} | |
named_scope :not_deleted, :conditions => {:deleted => false} | |
# Convenience class methods | |
def self.sent | |
author.not_deleted | |
end | |
def self.received | |
recipient.not_deleted | |
end | |
end | |
# Example usage | |
user = User.first | |
user.messages.sent | |
user.messages.received | |
user.messages.deleted |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment