I have a multi-step form that creates a Post and its Topics.
class Post < ActiveRecord::Base
has_many :post_topics
has_many :topics, through: :post_topics
end
class Topic < ActiveRecord::Base
has_many :post_topics
has_many :posts, through: :post_topics
belongs_to :parent, class_name: 'Topic'
has_many :subtopics, class_name: 'Topic', foreign_key: 'parent_id'
scope :subtopics, lambda { where.not(parent_id: nil) }
scope :topics, lambda { where(parent_id: nil) }
end
class PostTopic < ActiveRecord::Base
belongs_to :post
belongs_to :topic
end
Topic class has a self-referential association. I have a column parent_id
in Topics table. The ones with parent_id: nil are the root topics and the ones with a parent_id value are subtopics.
This is the form.
<%= simple_form_for(@post, method: :put, url: wizard_path) do |f| %>
<%= f.error_notification %>
<%= f.input :name, required: true, autofocus: true %>
<%= f.input :description, required: true %>
<%#= f.select :topic_ids, Topic.topics.collect {|x| [x.name, x.id]}, {}, label: "Select Topic" %>
<%= f.select :topic_ids, Topic.subtopics.collect {|x| [x.name, x.id]}, {}, :multiple => true, class: "multipleSelect", label: "Select Subtopic" %>
<%= f.button :submit, "NEXT", class: 'btn btn-danger' %>
<% end %>
The form looks like this
Name
Description
Select SubTopics
Name, Description and Select Subtopics fields save values as expected. What I want is another select input that lets users select a Topic (parent topic) (commented out that line in the above form)
Name
Description
Select Topic
Select SubTopics
How do I keep the same column(topic_ids) multiple times in this form so that
"Select Topic" input will save the parent topic
"Select Subtopics" input will save multiple subtopics