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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:18:53+00:00 2026-06-04T06:18:53+00:00

I have a form for creating a Style. When creating the style the user

  • 0

I have a form for creating a Style. When creating the style the user must also choose some features for the style.

The features are pre-populated into a multi-select box inside the form. When the style is saved the through table should be updated with entries (with style_id and feature_id) for each feature selected on the form.

I have in my controller:

def new
    @style = Style.new
    @style.stylefeatures.build
end

def create 
    @style = Style.new(params[:style])
    @style.stylefeatures.build
    @style.save
end

…and in my style model

  attr_accessible :stylefeatures_attributes
  has_many :stylefeatures
  has_many :features, :through => :stylefeatures, :foreign_key => :feature_id
  accepts_nested_attributes_for :stylefeatures

… and in my stylefeature model

  belongs_to :style
  belongs_to :feature
  accepts_nested_attributes_for :feature

… and in my feature model

  attr_accessible :description, :fullname, :name
  has_many :stylefeatures
  has_many :styles, :through => :stylefeatures, :foreign_key => :style_id

… and in my create form

<%= m.simple_fields_for :stylefeatures do |p| %>
  <%= p.input :feature_id, :label => "Features", :collection => Feature.all, :input_html => { :multiple => true } %>
<% end %>

Now when I save the new Style, the stylefeatures table is updated with the appropriate style_id, but with two useless entries. The first is an array with all of the feature ids which have been selected in the form. The second is a blank entry with the appropriate style_id and nothing in the feature_id column.

Do you have any clue of what I might be doing wrong, or how to spread the collected feature_id’s into the table as desired?

  • 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-04T06:18:54+00:00Added an answer on June 4, 2026 at 6:18 am

    in new action u r building only one stylefeature, so only one will be created with feature_id = ‘1,3,45,563’ (ofc depends that features u selected)
    second one is generated by @style.stylefeatures.build in create action

    u can try to remove fields_for and simply use :feature_ids instead of :feature_id

    <%= p.input :feature_ids, :label => "Features", :collection => @features, :input_html => { :multiple => true } %>
    

    or

    <%= input_tag "style[feature_ids][]" , :label => "Features", :collection => @features, :input_html => { :multiple => true } %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a multi-part form in the style that Ryan Bates describes here: http://railscasts.com/episodes/217-multistep-forms
I'm creating a form. As of right now I have simple MAILTO form that
I have astring directly coming form the database and I am creating object of
I'm creating a simple form and in this form I want to have an
I have a jQuery Model windows which contains a form. On creating of the
I have form where user submits field. Field can have letters, numbers, and punctuation.
I have form, where some fields are looks like rows, so I can add/delete
I have a simple dropdown menu with some options eg: <select name='music_style' id='palmsForm-music_style'> <option
I have a form for creating an album like following: # app/views/albums/new.html.erb <h2>Details of
I am creating a form wizard that guides the user through each form element,

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.