开发者

Retactoring advanced has_many example

My user model has three relations for the same message model, and is using raw SQL :/ Is there a better more rails way to achieve the same result?

Could the foreign key be changed dynamically? e.g User.messages.sent (foreign key = author_id) and User.messages.received (foreign key = recipient ) I have been trying to move some of the logic into scopes in the message model, but the user.id is not available from the message model...

Any thoughts?

Table layout:

  create_table "messages", :force => true do |t|
    t.string   "subject"
    t.text     "body"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "author_id"
    t.integer  "recipient_id"
    t.boolean  "author_deleted",    :default => false
    t.boolean  "recipient_deleted", :default => false
  end

This is my relations for my user model:

  has_many :messages_received, :foreign_key => "recipient_id", :class_name => "Message", :conditions => ['recipient_deleted = ?', false]
  has_many :messages_sent, :foreign_key => "author_id", :class_name => "Message", :conditions => ['author_deleted = ?', false]
  has_many :messages_deleted, :class_name => "Message", :finder_sql => 'SELECT * FROM Messages WHERE
                                                                        author_id = #{self.id} AND author_deleted = true OR
               开发者_开发百科                                                         recipient_id = #{self.id} AND recipient_deleted = true'

Best regards. Asbjørn Morell


Yes, use a named_scope for sorting between deleted and not deleted messages.

class User < ActiveRecord::Base
  has_many :messages_received, :foreign_key  => 'recipient_id'
  has_many :messages_sent, :foreign_key => 'author_id'
end

class Messages < ActiveRecord::Base
  named_scope :deleted, :conditions => 'author_deleted = TRUE OR recipient_deleted = TRUE' 
  named_scope :not_deleted, :conditions => 'author_deleted = FALSE OR recipient_deleted = FALSE'
end

# Example user
user = User.first
user.messages_received.deleted
user.messages_received.not_deleted
user.messages_sent.deleted
user.messages_sent.not_deleted

Alternatively, you could go one step further and simplfy the association by using the user_id as the foreign key and specifying the message type.

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

This approach is advantagoues because:

  1. One less column.
  2. Extendable. Adding an additional message type in the future is trivial (Eg: Drafts).
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜