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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:25:21+00:00 2026-05-23T09:25:21+00:00

With the following Rails models: class Group < ActiveRecord::Base has_many :group_locations, :dependent => :restrict

  • 0

With the following Rails models:

class Group < ActiveRecord::Base
  has_many    :group_locations, :dependent => :restrict
  has_many    :locations, :through => :group_locations, :dependent => :restrict
end

class Location < ActiveRecord::Base
  has_many        :group_locations, :dependent => :destroy
  has_many        :groups, :through => :group_locations
end

class GroupLocation < ActiveRecord::Base
  belongs_to  :group
  belongs_to  :location
end

I am trying to Find all of the locations that are associated with at least one of several groups contained in the string “group_list” (e.g. “1,2,3,4,5”). If it was a field from the Location record, I would specify a condition of “*field in (#{group_list})*“. But how do I accomplish my goal when I want to have at least one of the location’s “group_location” whose “group_id” is in the list (or, alternatively, one “group” whose group_id is in the list).

I know how to do it with pure SQL, but how do you do it with Rails?

  • 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-23T09:25:22+00:00Added an answer on May 23, 2026 at 9:25 am

    @taro You are right. Started by adding the code

    joins(:group_locations).where("group_id in (?)", group_id_array)
    

    Then I proceeded to define a scope just to make it a nice package:

    scope :locations_in_groups, lambda { |grparray| joins(:group_locations).where("group_id in (?)", grparray) }
    

    Thanks for your help.

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

Sidebar

Related Questions

I have a the following rails models: class Release < ActiveRecord::Base has_many :release_questionnaires, :dependent
Given the following models: class Recipe < ActiveRecord::Base has_many :recipe_ingredients has_many :ingredients, :through =>
I have the following models: class User < ActiveRecord::Base has_many :subscriptions end class Subscription
I have the following three models (Rails 2.3.8) class Outbreak < ActiveRecord::Base has_many :incidents,
I have the following models class Project < ActiveRecord::Base has_many :project_members has_many :members, :through
Models: class User < ActiveRecord::Base has_many :attendances has_many :courses, :through => :attendances end class
I have the following models: class User < ActiveRecord::Base has_many :books, :dependent => :destroy
I have the following models: class Instance < ActiveRecord::Base has_many :users has_many :books end
I have a rails 3.1 app with the following Models: class Post < ActiveRecord::Base
Hi (huge Rails newbie here), I have the following models: class Shop < ActiveRecord::Base

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.