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

  • SEARCH
  • Home
  • 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 4579446
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T20:38:35+00:00 2026-05-21T20:38:35+00:00

I am new to rails. Having a blast. The query API though is giving

  • 0

I am new to rails. Having a blast. The query API though is giving me
some trouble. I’ve been zooming and doing a lot of stuff very quickly,
but this is the first time I have spent hours trying to figure it out.
It’s not like anything I’ve used before – regular SQL, or Hibernate,
or whatever.

The model I have is pretty simple.

  • A PrivateMessage has many Recipients
  • A Recipient has a Receiver (which of class User)
    • recipient also has fields for ‘is_read’ and ‘is_deleted’

My goal is to build a query that finds all the unread and not deleted
private messages for a given user. To accomplish this, we need to join
‘private_messages’ to ‘recipients’… and then ‘recipients’ to
‘users’.

Here’s the relevant User model code:

has_many :sent_messages, :class_name => 'PrivateMessage', :foreign_key => 'sender_id'
has_many :recipient_of_messages, :class_name => 'Recipient', :foreign_key => 'receiver_id'

scope :by_id, lambda { |id| where(:id => id) } 

My Recipient model has the following relevant code:

belongs_to :receiver, :class_name => 'User', :foreign_key => "receiver_id"
belongs_to :private_message

scope :unread, where(:is_read => false).where(:is_deleted => false)
scope :by_receiver_id, lambda { |id| Recipient.joins(:receiver).merge(User.by_id(id)) }
scope :unread_by_receiver_id, lambda { |id| unread.by_receiver_id(id) }

When tested in isolation, this works 100%.

However, when I code the private message queries, I run into problems.

belongs_to :sender, :class_name => 'User'
has_many :recipients, :class_name => 'Recipient'

scope :sorted, order("private_messages.created_at desc")
scope :non_deleted, where(:is_deleted_by_sender => false)
scope :non_deleted_by_sender_id, lambda { |id| sorted.non_deleted.joins(:sender).merge(User.by_id(id)) }

# this scope does not work
scope :non_deleted_by_receiver_id, lambda { |id| sorted.joins(:recipients).merge(Recipient.by_receiver_id(id)) }
scope :newest, sorted.limit(3)

# this scope does not work either
scope :newest_unread_by_receiver_id, lambda { |id| newest.joins(:recipients).merge(Recipient.unread_by_receiver_id(id)) }

When I try and use ‘newest_unread_by_receiver_id’ or ‘non_deleted_by_receiver_id’, I get the following exception:

ActiveRecord::ConfigurationError: Association named 'receiver' was not found; perhaps you misspelled it?

This doesn’t make much sense to me… because if the name was spelled
wrong, why doesn’t it fail when I test it isolation?

Can someone help me out please? This one is driving me nuts. At times
like this, I just want to program in full sql or Hibernate QL so I could just be done with it 🙁

If I’m just approaching the problem totally wrong, then I’d appreciate it if you just let me know that too. I am under the impression that using scopes and ActiveRelation was the way moving forward in Rails 3.1.

Thanks

  • 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-21T20:38:36+00:00Added an answer on May 21, 2026 at 8:38 pm

    I would probably use something like this. I kept scopes separate for clarity.

    Models (renamed PrivateMessage -> Message and Recipient -> MessageCopy):

    class User < ActiveRecord::Base
      has_many :sent_messages, :class_name => "Message", :foreign_key => :sender_id
      has_many :sent_message_copies, :through => :sent_messages, :source => :message_copies
      has_many :received_messages, :through => :received_message_copies, :source => :message
      has_many :received_message_copies, :class_name => "MessageCopy", :foreign_key => :recipient_id
    end
    
    class Message < ActiveRecord::Base
      belongs_to :sender, :class_name => "User"
      has_many :message_copies
      has_many :recipients, :through => :message_copies
    end
    
    class MessageCopy < ActiveRecord::Base
      belongs_to :message
      belongs_to :recipient, :class_name => "User"
      scope :unread, where(:read => false)
      scope :undeleted, where(:deleted => false)
      scope :sent_to, lambda { |recipient| where(:recipient_id => recipient.id) }
    end
    

    Schema (migrations would have taken too much space here):

    ActiveRecord::Schema.define(:version => 20110503061008) do
      create_table "message_copies", :force => true do |t|
        t.boolean  "read",         :default => false
        t.boolean  "deleted",      :default => false
        t.integer  "message_id"
        t.integer  "recipient_id"
      end
      create_table "messages", :force => true do |t|
        t.string   "title"
        t.integer  "sender_id"
      end
      create_table "users", :force => true do |t|
        t.string   "name"
      end
    end
    

    –edit

    Example query using joins returning messages

    Message.joins(:message_copies).where(:message_copies => {:read => false, :deleted => false, :recipient_id => 3})
    

    Message scope reusing scopes on other model

    scope :non_deleted_by_recipient, lambda { |recipient|
      joins(:message_copies).merge(MessageCopy.unread.undeleted.sent_to(recipient))
    }
    

    –edit2

    This Railscast has nice examples of both joins and scopes:

    • http://railscasts.com/episodes/215-advanced-queries-in-rails-3
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm completely new to rails and I'm having a lot of trouble getting my
I'm new to rails and I'm having some trouble in the console. I'd like
I am new to rails and I having trouble using cucumber to run my
I'm brand new to ruby and rails and I'm having trouble creating a sign-out
I'm new to rails and having trouble figuring out how to do this RESTfully.
I'm very new to Rails (and Ruby), and am having trouble installing and using
I'm fairly new to Rails, and am having trouble getting the value of a
I was having some trouble converting the following route from rails 2 way to
I'm very new to rails and I've been following a lot of great tutorials
I am new to rails and am having troubles figuring out some things with

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.