Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 673913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:38:53+00:00 2026-05-14T00:38:53+00:00

My user model has three relations for the same message model, and is using

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T00:38:54+00:00Added an answer on May 14, 2026 at 12:38 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 368k
  • Answers 368k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The Entity Framework designer is terrible - I've had the… May 14, 2026 at 5:11 pm
  • Editorial Team
    Editorial Team added an answer If by "hijack" you meant sniff the packets then what… May 14, 2026 at 5:11 pm
  • Editorial Team
    Editorial Team added an answer If you want two actions to be atomic, embed them… May 14, 2026 at 5:11 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.