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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:29:05+00:00 2026-06-17T08:29:05+00:00

This is in continuation to the question which was raised here How to add

  • 0

This is in continuation to the question which was raised here

How to add one-to-many objects to the parent object using ActiveRecord

class Foo < ActiveRecord::Base
  has_many :foo_bars
end

class Bar < ActiveRecord::Base
end  

class FooBar < ActiveRecord::Base
  belongs_to :foo
  belongs_to :bar
end 

How to handle removal of entries in a multi select check box are used to represent one-to-many entities. I am able to add or update entries, but removal seems to fail since foo_id seems to be empty and the query seems to be updating instead of delete.

EDIT :

I tried with @charlysisto suggestion using the following code

My Controller code is as follows :

  class Foo < ActiveRecord::Base
    has_many :foo_bars
    has_many :bars, :through => :foo_bars
  end

   def edit
    @foo = Foo.find(params[:id])
    @sites = Site.where(company_id: @current_user.company_id).all
   end

  def update
    @foo = Foo.find(params[:id])

    if @foo.update_attributes(params[:foo])
      flash[:notice] = "Foo was successfully updated"
      redirect_to foos_path
    else
      render :action => 'edit'
    end
   end

View code is as follows :

<% @bars.each do |bar| %>
    <%= check_box_tag 'bar_ids[]', bar.id %>
    <%= bar.name %>
<% end %>

So I tried with these changes, but still foo_bars doesn’t seem to reflect the changes if I removed a record.

  • 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-17T08:29:06+00:00Added an answer on June 17, 2026 at 8:29 am

    What’s missing in your associations is this :

    class Foo < ActiveRecord::Base
      has_many :bars, :through => :foo_bars
    end
    

    … the has_many (or has_many :through) macro gives you a truck load of methods including bar_ids and bar_ids= [...]

    So all you need to do in your views/controller is :

    # edit.html.haml which will send an array of bar_ids
    =f.select :bar_ids, Bar.all.map {|b| [b.name, b.id]}, {}, :multiple => true
    
    # foo_controller
    @foo.update_attributes(params[:foo])
    

    And that’s it! No need to manually set or delete the associations in FooController#update…

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

Sidebar

Related Questions

This a continuation of this question. I have an Address class which contains basic
This is a continuation of this question from yesterday . Here are my three
This is a continuation of the question here: JBoss - does app have to
This question is a continuation of my previous question here zend models architecture (big
This is in continuation to my previous question which was closed cause of similarity
This is a continuation of my previous question which I posted when I wasn't
In continuation of the question at this link I wanted to add some functionality
This is a continuation of this question Model class and Mapping I had my
This question is in continuation of this one . The deal is simple. Given:
This question is continuation of my question . Here is problematic code. a.h: #include

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.