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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:12:34+00:00 2026-06-09T21:12:34+00:00

I’m creating a Rails app that communicates with an old SQL database and I

  • 0

I’m creating a Rails app that communicates with an old SQL database and I need to set the primary_key.

Intially I did this:

class Post < ActiveRecord::Base
  attr_accessible :body, :title, :incrementer_id

  self.primary_key = "incrementer_id"

  before_create :next_seq, on: :create

  private

  def next_seq
    p = Post.last
    self.incrementer_id = p.blank? ? 1 : (p.incrementer_id.to_i + 1)
  end

end

I needed to create the next_seq because Rails doesn’t automatically increase the ‘incrementer_id’.

I had to make the ternary because the first item’s incrementer_id is nil and it was giving error. I would love to avoid having to make this check every time.

I wonder if there’s an optimal way to do the next_seq. Even better, make it database-type independent.

  • 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-09T21:12:35+00:00Added an answer on June 9, 2026 at 9:12 pm

    First of all, I wouldn’t count on the Post.last method to give you the item with the highest numbered ID. For that, you would Post.maximum(:id). In Ruby, since && and || work with any kinds of values, not just booleans, and nil is treated as falsey, you can say…

    self.incrementer_id = (Post.maximum(:id) || 0) + 1
    

    Doing this involves an extra round trip to the database for every save, and finding the maximum existing ID might not be terribly efficient. If you write a lot of these records, you might consider using a pool of IDs in memory. To do that, you need a row in another table that stores the next available ID, and the first time your app needs a new one, increment that by some value such as 10 or 100, in effect reserving that many IDs for use by your application instance. Assign ID values from that range until it is used up, and then acquire a new pool of IDs as before.

    If you use the pooling strategy and the database is shared by another app that computes new IDs by adding 1 to the maximum existing ID, you can make your strategy compatible with that by assigning IDs in descending order from the pool.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
We're building an app, our first using Rails 3, and we're having to build
I know there's a lot of other questions out there that deal with this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

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.