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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:41:11+00:00 2026-05-21T06:41:11+00:00

I was implementing my first HABTM relationship and have run into an issue with

  • 0

I was implementing my first HABTM relationship and have run into an issue with my query.

I am looking to validate my approach and to see if I have found a bug in the AREL (or some other part of Rails) code.

I have the following models

class Item < ActiveRecord::Base
  belongs_to :user
  belongs_to :category
  has_and_belongs_to_many :regions
end

class Region < ActiveRecord::Base
  has_ancestry
  has_and_belongs_to_many :items
end

I have the associated items_regions table:

class CreateItemsRegionsTable < ActiveRecord::Migration
  def self.up
    create_table :items_regions, :id => false do |t|
      t.references :item, :null => false
      t.references :region, :null => false
    end
    add_index(:items_regions, [:item_id, :region_id], :unique => true)
  end

  def self.down
    drop_table :items_regions
  end
end

My goal is to create a scope/query is follows:

Find all items in a region (and its subregions)

The ancestory gem provides a method to retrieve descendant categories for Region as an array. In this case,

ruby-1.9.2-p180 :167 > a = Region.find(4)
 => #<Region id: 4, name: "All", created_at: "2011-04-12 01:14:00", updated_at: "2011-04-12 01:14:00", ancestry: nil, cached_slug: "all"> 
ruby-1.9.2-p180 :168 > region_list = a.subtree_ids
 => [1, 2, 3, 4] 

If there is only one element in the array, the following works

items = Item.joins(:regions).where(["region_id =  ?", [1]])

The sql generated is

"SELECT `items`.* FROM `items` INNER JOIN `items_regions` ON `items_regions`.`item_id` = `items`.`id` INNER JOIN `regions` ON `regions`.`id` = `items_regions`.`region_id` WHERE (region_id =  1)"

However, if there are multiple items in the array and I try to use IN

Item.joins(:regions).where(["region_id IN  ?", [1,2,3,4]])
ActiveRecord::StatementInvalid: Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1,2,3,4)' at line 1: SELECT `items`.* FROM `items` INNER JOIN `items_regions` ON `items_regions`.`item_id` = `items`.`id` INNER JOIN `regions` ON `regions`.`id` = `items_regions`.`region_id` WHERE (region_id IN  1,2,3,4)

The sql generated has an error at the end

"SELECT `items`.* FROM `items` INNER JOIN `items_regions` ON `items_regions`.`item_id` = `items`.`id` INNER JOIN `regions` ON `regions`.`id` = `items_regions`.`region_id` WHERE (region_id IN  1,2,3,4)"

the last part of the generated code should be
(region_id IN (“1,2,3,4”))

If I edit the sql manually and run it, I get what I expect.

So, two questions:

  1. Is my approach for the single value case correct?
  2. Is the sql generation a bug or have I configured things incorrectly?

Thanks
Alan

  • 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-21T06:41:12+00:00Added an answer on May 21, 2026 at 6:41 am
    .where('regions.id' => array)
    

    Should work in all cases, whether or not you specify one value or multiple.

    The reason your original query doesn’t work is that you actually need to specify valid SQL. So alternatively you can do

    .where('region_id IN (?)', [1,2,3,4])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.