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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:38:46+00:00 2026-06-02T16:38:46+00:00

I have the following model: class Entry include Mongoid::Document field :title, type: String field

  • 0

I have the following model:

class Entry
  include Mongoid::Document
  field :title, type: String
  field :description, type: String
  field :made, type: Date
  embeds_many :images
  embeds_many :videos
  embeds_many :files
  embeds_many :tags
  accepts_nested_attributes_for :images, :videos, :files, :tags
  validates_presence_of :title, :description, :tags
  validates_uniqueness_of :title, :description
end

class Tag
  include Mongoid::Document
  field :tag, type: String
  embedded_in :entry
  embedded_in :note
end

The post route looks like this:

post '/portfolio/new' do
  a = (params[:entry])
  a['tags_attributes']['0']['tag'].downcase.split(", ").each_with_index{|value, index| a['tags_attributes'][index.to_s] = {"tag" => value} }
  b = Entry.new(a)
  b.safely.save!
  redirect "portfolio/show/#{b._id}"
end

and my haml input looks like:

%label{:for => "tags"} Tags:
%input{:name => "entry[tags_attributes[0[tag]]]"}

I’m new at Ruby/Sinatra/Mongoid, so I am still trying to figure out how to access documents attributes properly.

What I am trying to do is to process the http post information and be able to (almost immediately) save it to mongodb.

The haml method for placing the input values in the right place on the hash is one I found to work through trial and error. But it doesn’t feel DRY, surely there is a better way write the embedded document? specifically entry[tags_attributes[0[tag]]] feels very akward, is there a better way of writing this?

Also in my route, in order to break the string of tags I have and store it back into the hash structure as individual embedded document before saving. I feel it’s a very round-about-way of parsing this information.

What are the best practices for dealing with 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-06-02T16:38:48+00:00Added an answer on June 2, 2026 at 4:38 pm

    Haml

    %input{name: 'entry[tags_attributes][][tag]', value: t.tag}
    

    Route simply

    post '/portofolio/new' do
      Entry.new(params[:entry])
      ...
    end
    

    Be aware on edit/put don’t forget the embedded id

    %input{type: 'hidden', name: 'entry[tags_attributes][][id]', value: t.id}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following models: class Tag(models.Model): name = models.CharField(max_length=20) class Entry(models.Model): title =
Let's say I have the following model: class Contest: title = models.CharField( max_length =
Consider I have defined the following models: class Tag(models.Model): name = models.CharField(max_length=20) class Entry(models.Model):
I have the following models: class LibraryEntry(models.Model): player = models.ForeignKey(Player) player_lib_song_id = models.IntegerField() title
Let's say I have the following Django model: class Info(models.Model): instrument = models.ForeignKey('Instrument') date
I have the following model class (simplified) class A{ private String name; private List<B>
I have the following routing configuration: entry: class: sfDoctrineRouteCollection options: model: ProjectEntry module: entry
Assume the following: models.py class Entry(models.Model): title = models.CharField(max_length=50) slug = models.CharField(max_length=50, unique=True) body
Using the following related models (one blog entry can have multiple revisions): class BlogEntryRevision(models.Model):
I have a following model: class Car(models.Model): make = models.CharField(max_length=40) mileage_limit = models.IntegerField() mileage

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.