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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:58:49+00:00 2026-05-26T01:58:49+00:00

I am in rails 3.1. I have the following models class Tool < ActiveRecord::Base

  • 0

I am in rails 3.1. I have the following models

    class Tool < ActiveRecord::Base
        has_many :comments
    end

    class Comment < ActiveRecord::Base
        belongs_to :tool
        has_many :relationships
        has_many :advantages, :through => :relationships, :source => :resource, :source_type => 'Advantage'
        has_many :disadvantages, :through => :relationships, :source => :resource, :source_type => 'Disadvantage'

    end

    class Relationship < ActiveRecord::Base
        belongs_to :comment
        belongs_to :resource, :polymorphic => true
    end

    class Disadvantage < ActiveRecord::Base
        has_many :relationships, :as => :resource
        has_many :comments, :through => :relationships
    end

    class Advantage < ActiveRecord::Base
        has_many :relationships, :as => :resource
        has_many :comments, :through => :relationships
    end

In short, A Tool has many comments. A Comment inturn is associated with Advantages and Disadvantages. So in my tool/show page, I would list out all the comments.

But if I have to add comment to the tool page, there would be a form which has a textarea for comment and two multi select list boxes for advantages and disadvantages.

And here is the catch, if the user wanna select from existing adv/disadv, the user can select from the list box or if the user wants to a add a new adv/disadv he can type it and add it, so that it is saved thru an ajax call and the new adv/disadv is added to the list box. How am I supposed to do this?

  • 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-26T01:58:50+00:00Added an answer on May 26, 2026 at 1:58 am

    What you’re looking for are “nested forms” – they are really straight-forward to use.

    in your Gemfile add:

    gem "nested_form"
    

    1) in your main_model, you’ll include a call to accepts_nested_attributes_for :nested_model

    class MainModel
      accepts_nested_attributes_for :nested_model
    end
    

    2) in the view for your main_model instead of form_for() , you will call nested_form_for() at the top

    = nested_form_for(@main_model) do |f|
       ...
    

    Check the Rails page for that method, it has interesting options, e.g. :reject_if , :allow_destroy, …

    3) in the view for the main_model, when you want to show a sub-form for the nested model, you will do

    = f.fields_for :nested_model   # replace with your other model name
    

    it will then just use the _form partial for the nested_model and embed it in the view for the main_model

    works like a charm!

    Check these RailsCast.com episodes, which cover Nested Forms in depth:

    http://railscasts.com/episodes/196-nested-model-form-part-1

    http://railscasts.com/episodes/197-nested-model-form-part-2

    hope this helps

    • 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
I have the following models in my Rails application: class Shift < ActiveRecord::Base has_many
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 Instance < ActiveRecord::Base has_many :users has_many :books end
I have the following two models: class PhotoAlbum < ActiveRecord::Base belongs_to :project has_many :photos,
Hi (huge Rails newbie here), I have the following models: class Shop < ActiveRecord::Base
I have the following models: class Match < ActiveRecord::Base has_and_belongs_to_many :teams end And class
I have the following models: class Mark < ActiveRecord::Base validates_presence_of :user validates_presence_of :page belongs_to
I have a rails 3.1 app with the following Models: class Post < 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.