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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:48:31+00:00 2026-05-27T16:48:31+00:00

Just trying out Mongoid at the moment, I’ve run into an issue that’s probably

  • 0

Just trying out Mongoid at the moment, I’ve run into an issue that’s probably pretty simple but has me at a loss:

I have a really simple Article model:

class Article
  include Mongoid::Document
  field :title, :type => String
  field :content, :type => String

  key :title

  referenced_in :subject

  validates_presence_of :title
end

I added key :title after I had already created one test record. Newly created records work as expected, but the first Article (which originally had the normal mongoid object id) behaves strangely:

In rails views this first article still returns its object id instead of the new key. ie using link_to article.name, article returns:

<a href="/articles/4ef150970a68b38415000003">Show</a>

… when all the rest return the parameterized keys, like:

<a href="/articles/other-article">Show</a>

If I click that link I get “Record not found”. I tried loading and resaving this record in the console, and after that calling article.id on that record did return the parameterized key, but it still shows up the old way in the view and doesn’t work.

So, a couple questions:

  1. What’s going on here?
  2. How do you fix it?
  3. This situation indicates to me that if you set a field on a mongoid model to be the key, you need to be really sure that it will never change. How do you handle something like using the title of an article as a slug, then, when those may occasionally need change?

Thanks!

  • 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-05-27T16:48:32+00:00Added an answer on May 27, 2026 at 4:48 pm

    Well, since _id is immutable, your only option is to reinsert this document with your new ‘sluggish’ id and delete the old one.

    And yes, _id format and shard key (if you use sharding) are the two things you better have right from the beginning 🙂
    Everything else can be fixed relatively easily.

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

Sidebar

Related Questions

I'm just starting out writing trying to write a simple program in C and
I've just starting trying out eclipse BIRT to generate reports, but am unable to
I'm just trying to work out the best way to replicate a pretty useful
Just trying out this IDE but it already looks better than NetBeans in terms
I'm just trying out a sample that gave the above error message 21 Dec,
Just trying to figure out what's the best practice: when using method that takes
I just started trying out RavenDB and it seems that the new/easy way to
Just trying out Hibernate (with Annotations) and I'm having problems with my mappings. I
I'm just trying out the allegro library, and here is the code which I've
I'm just trying to work out why the following code is leaking memory and

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.