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 7903505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:46:01+00:00 2026-06-03T09:46:01+00:00

Our Rails 3 application has models Person and Message . Messages can be specific

  • 0

Our Rails 3 application has models Person and Message. Messages can be specific to a Person (when the message person_id column is set) or they can be “global” (when the person_id column is NULL).

We would like to have a simple has_many relationship using the :conditions option as such:

class Person < ActiveRecord::Base
  has_many :messages,
      :conditions => proc { ['(messages.person_id IS NULL) OR ' +
                             '(messages.person_id = ?)'], self.id }
  # ...
end

But it appears that the has_many class method encodes the “conditions” option as a logical “AND” clause after enforcing the foreign key constraint of equality to the Person object’s id (e.g. “FROM messages WHERE person_id=123 AND (person_id IS NULL OR person_id=123)“). It appears that there is no way to allow associated objects with null foreign keys to belong to such associations.

Does Rails 3 / ActiveRecord provide a way to do this or must I hack my own association-like methods?

  • 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-06-03T09:46:01+00:00Added an answer on June 3, 2026 at 9:46 am

    You can’t have an OR clause like you want using conditions on the ActiveRecord assocation. You could have the association without conditions and then add a method to include the global messages you want. That way you could still take advantage of the association when building associated records.

    # person.rb
    has_many :messages
    
    def all_messages
      Message.where('messages.person_id=? OR messages.person_id IS NULL', id)
    end
    

    Here’s my standard plug for the Squeel gem, which is handy for more advanced queries like this if you don’t want to have bits of SQL in your code. Check it out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After our Ruby on Rails application has run for a while, it starts throwing
We're hosting our Ruby on Rails application with the EngineYard App Cloud, which has
We're using EngineYard Cloud to deploy our Ruby on Rails application. We are running
I need to set up a payment solution within one of our Rails apps
I'm sure someone can explain this. we have an application that has been in
I have created a pdf version of our rails application using the Prawn plugin,
As I said in a previous post, our Rails app has to interface with
I have a Ruby on Rails application that has two active environments, Stage and
Our team is working on a new application that we started with Rails 3.1
We just found something strange with our rails 3.1.3 app's application.js. The problem is

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.