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

The Archive Base Latest Questions

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

I have a List model. Lists have embedded Tags (using Mongoid). When a user

  • 0

I have a List model. Lists have embedded Tags (using Mongoid). When a user creates a list, he can specify associated tags via comma separated list in a text field.

How do I store the tags through the List association? Can I do it with accepts_nested_attributes_for :tags on the List model or do I have to pre-process the tags string?

Here’s what I have so far. How do I deal with the tags string, splitting it and storing each tag individually in the embedded tag doc that’s part of list?

List controller:

class ListsController < ApplicationController
  def new
    @list = List.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @list }
    end
  end

  def create
    list_params = params[:list]
    list_params[:user_id] = current_user.id
    @list = List.new(list_params)
    if @list.save
     redirect_to @list, notice: 'List was successfully created.'
    else
      render action: "new"
    end
  end
end

List creation form

= form_for @list do |f|
  - if @list.errors.any?
    #error_explanation
      %h2= "#{pluralize(@list.errors.count, "error")} prohibited this list from being saved:"
      %ul
        - @list.errors.full_messages.each do |msg|
          %li= msg

  .field
    = f.label :name
    = f.text_field :name
  .field
    = f.label :description
    = f.text_field :description
  .field
    = f.fields_for :tags do |t|
      = t.label :tags
      = t.text_field :name
  .actions
    = f.submit 'Save'

List model

class List
  include Mongoid::Document
  include Mongoid::Timestamps
  field :name
  field :description
  embeds_many :items
  embeds_many :comments
  embeds_many :tags
  belongs_to :user

  accepts_nested_attributes_for :tags

Tag model

class Tag
  include Mongoid::Document
  field :name
  has_one :list
end

Edit based on Geoff’s suggestion

List controller ultimately looked.

 def create
    tags = params[:tags][:name]
    list = params[:list]
    list[:user_id] = current_user.id
    @list = List.new(list)
    tags.gsub("\s","").split(",").each do |tag_name|
      @list.tags.new(:name => tag_name)
    end
    if @list.save
     redirect_to @list, notice: 'List was successfully created.'
    else
      render action: "new"
    end
  end
  • 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:36:11+00:00Added an answer on June 17, 2026 at 8:36 am

    If I understand you correctly, here’s what I think you want to do:

    Your view as it is will display a tag name field for each tag, but there are no associated tags at create time so I assume it just doesn’t show anything.

    When using a :has_many, there is a nice little function that will be created for you by rails called List.tag_ids. Since I am not familiar with it, I am assuming the functionality of :embeds_many is a superset of of :has_many. The point of this is that if you can provide some sort of form which gathers the ids, then you’d be done. A set of checkboxes could work, or a multi-select. Here is what checkboxes might look like:

    = hidden_field_tag "list[tag_ids][]"
    - Tags.all.each do |t|
      = check_box_tag "list[tag_ids][]", t.id, t.list == @list, id: "list_tag_ids_#{t.id}"
      = label_tag "list_tag_ids_#{t.id}", t.name
    

    I asked something similar here:

    Rails: How do I transactionally add a has_many association to an existing model?

    However, this isn’t what you asked for. You could achieve a form which associates tags by name by using javascript. That would probably be nice, but it would be a bit tricky.

    Assuming no javascript, if you are inputting the names as a comma separated list in the view, you will need to parse it in the controller. Similar to this:

    View:

    label_tag :tags
    text_field_tag :tags
    

    Controller:

    ...
    @list = List.new(list_params)
    params[:tags].gsub("\s","").split(",").each do |tag_name|
      tag = Tag.find_by_name(tag_name)
      @list.tags << tag if tag
    if @list.save
    ...
    

    None of the solutions I suggested make use of nested attributes. I don’t think they are applicable for you here. They are used when you are trying to update the attributes of the nested model, but you’re just trying to make the association.

    I hope that helps.

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

Sidebar

Related Questions

I have an List document with embedded Comments. Along with the user's comment in
assume that i have a BlogPost model with zero-to-many embedded Comment documents. can i
I have a GWT 2.3 web app using Objectdb via Rpc. In the embedded
I have a select list in my model which lists a persons name with
I have two lists List<User> list_of_users=new ArrayList<User>(); List<String> list_of_attributes=new ArrayList<String>(); When i try to
I have the following: MODEL.PY LIST = (('Manager', 'Manager'),('Non-Manager', 'Non-Manager'),) class Employee(models.Model): fname =
I'm building a model to list the students that have a birthday during the
I have a list of objects in a model. I wish to show elements
In my view model if have a: List<Car> where car has an Id and
I have a list of type IList<Effort> . The model Effort contains a float

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.