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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:06:25+00:00 2026-06-13T17:06:25+00:00

I am using Rails 3.1.0 I have two models, A and B. A has_one

  • 0

I am using Rails 3.1.0

I have two models, A and B.
A has_one B, but B can be null.
B belongs_to A.
B has a boolean field called “visible.”

I want to get all A records that have a non-null B and a B with “visible” set to true.

What is a concise and efficient way of doing this query?

What I’ve done so far:

I created a scope that gets the A’s with non-null B’s (I think).

scope :has_b, includes(:b).where(B.arel_table[:id].not_eq(nil))

Is there a way to chain the visible == true condition to this scope?

  • 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-13T17:06:26+00:00Added an answer on June 13, 2026 at 5:06 pm
    A.includes(:bs).where(:bs => { :visible => true })
    

    Zach Kemp was almost right

    So, in scope:

    scope as_with_visible_bs includes(:bs)
      .where(B.arel_table[:id].not_eq(nil))
      .where(:bs => { :visible => true })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a legacy project using Rails 2.3.5 but I can't find with which
I'm using Ruby 1.8.7 and Rails 2.3.8 I have two ActiveRecord Models, A and
I'm using MetaSearch gem in my Rails 3 project. I have two models: class
I'm using rails 3 and I have two models, venues and areas where each
I have a rails app with two models, visits and users. A visit has
I'm using Rails' accepts_nested_attributes_for method with great success, but how can I have it
I have two models, Character and Background. Character has_one Background, and Background belongs_to Character.
My Rails 3.1 app has two user models: User and Admin. I'm using Devise.
I'm tring to set values for two models that have a has_one association using
Using Rails 3.2. I have an individual Shop post, and each shop has reviews.

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.