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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:48:58+00:00 2026-06-01T01:48:58+00:00

Currently, I can add the creator_id like this in my controller: @entry = Entry.new(params[:entry].merge(:creator

  • 0

Currently, I can add the creator_id like this in my controller:

@entry = Entry.new(params[:entry].merge(:creator => current_user._id))

If this is my model:

class Entry
  include Mongoid::Document

belongs_to :User

  field :creator, :type => String
  field :title, :type => String
  field :content, :type => String
  field :scorea, :type => Integer
  field :scoreb, :type => Integer
  field :scorec, :type => Integer
end

Is there a better way 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-06-01T01:49:00+00:00Added an answer on June 1, 2026 at 1:49 am

    Your model doesn’t looks very good, do you really want to store the user_id in a string field?

    I suggest you change your models to following:

    class Entry
      include Mongoid::Document
    
      belongs_to :creator, :class_name => 'User', :inverse_of => :entries
    
      # field definitions
    end
    
    class User
      include Mongoid::Document
    
      has_many :entries, :inverse_of => :creator
    end
    

    Once you change the models you can continue using what you are now or alternatively:

    @entry = current_user.entries.build(params[:entry])
    

    Update:

    The method to initialize entry is not much different in the way I did it. It is just more towards the rails way of doing things. The main difference is that you were not using the associations. From your model definitions it is clear that you want a one-to-many association between user and entries and this is how you create such associations. Associations has a lot of goodies attached to them, like you can do following things:

    user.entries << entry # add a entry to users, will automatically change entry.creator_id
    entry.creator = user  # sets creato_id = user_id
    entry.creator         # returns associated user. no need to do User.find(entry.creator_id)
    user.entries          # returns all entries for use <=> Entry.where(creator_id: user.id)
    

    for more details go to http://mongoid.org/docs/relations.html

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

Sidebar

Related Questions

Currently I am trying to figure out how I can add dynamic query string
I am working a project currently in which the user can add multiple news
I was wondering if there was a function that I can add to this,
In Qt creator, I can start a new project with Perforce version control. But
Currently my users can add locations to their profiles via a form which includes
Currently, I have a table view that the user can add items and delete
In the classic VCSs (CVS/SVN) I can add new files/folders in one branch and
Do anyone know how I can add the following to form_alter? Currently I've got
How you can add a child ViewController into Master View Controller ? I have
I am currently building a web application for my work and you can add

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.