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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:46:36+00:00 2026-05-21T11:46:36+00:00

I have a model Foo and a model Bar, both of which has_many :bar_foos

  • 0

I have a model Foo and a model Bar, both of which has_many :bar_foos (and then has_many each other :through => :bar_foos). It’s a simple many:many relationship between Foo and Bar, through a BarsFoo model.

I want to delete several rows in the bar_foos table. Specifically, I want to delete any bar_foo record that relates to any of a given set of Foos and any of a given set of Bars. For performance, I’d like to issue a single database call to do this.

The corresponding SQL statement would be:

DELETE FROM bar_foos WHERE bar_id IN ( ?, ?, ? ) AND foo_id in ( ?, ?, ? )

…while replacing the ?s with the appropriate IDs.

How do I perform this using Rails 2.3.x?

Ideally, I’d to not write the actual SQL myself, but make ActiveRecord method calls instead. However, I doubt this exists.

Barring that, I’ll craft the SQL statement myself, but:

  • I’d like to avoid hardcoding the table/column names in the SQL string
  • I’d like to use some sort of automatic (ie: reliable) method to escape the SQL parameters vs. just concatenating strings.
  • 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-21T11:46:36+00:00Added an answer on May 21, 2026 at 11:46 am

    I think it’s not possible using assosciation you have to write it your own (Use destroy_all)

    BarFoo.destroy_all(["bar_id IN (?) AND foo_id in (?)", bar_id_array, foo_id_array]) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have model Foo which has field bar. The bar field should be unique,
I have a model with nested attributes : class Foo < ActiveRecord::Base has_many :bar
I have a model Foo which :has_many bars , initially I create and save
I have a model Foo that has_many 'Bar'. I have a factory_girl factory for
I have an ActiveRecord model class Foo that has_many Bar . I want to
Simple Rails question. I have a model Foo which looks like this: class Foo
I have an issue where I have a parent model Foo , which both
I have a model called Nonsense which has_one of Foo and Bar In my
We have 3 entities that relate to each other as such: Foo has many
Suppose I have class Foo(db.Model): bar = db.ReferenceProperty(Bar) foo = Foo.all().get() Is there a

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.