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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T01:37:01+00:00 2026-06-19T01:37:01+00:00

Considering the following :company has_many :issues #ticket tracking example I’d like to have routing

  • 0

Considering the following

:company has_many :issues  #ticket tracking example

I’d like to have routing such that any user in the company can go to /issues/:id (which is simple enough when using the default id column).

However, i’d like instead to have an issue id specific to the company (so each company would have their own issue 1, 2, 3 etc that isn’t unique (and wouldn’t use the internal id).

Is there any better way than calculating an id based on the last number in the db in the create and update actions in IssueController for that company id? (I can think of various issues here with race conditions when multiple records are being updated/created per company).

Thanks in advance!

  • 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-19T01:37:02+00:00Added an answer on June 19, 2026 at 1:37 am

    I would take the issue_id handling down to the model level. You can use a before_validation callback to set the issue_id. This alone, even though the save call is wrapped in a transaction, won’t prevent race conditions. You have to further ensure the uniqueness of the couple [ :company_id, :issue_id ] by adding a index/unique constraint to your issues table, as suggested by @PinnyM. So something like this

    class Issue < ActiveRecord::Base
      attr_accessible :company_id, :issue_id
      belongs_to :company
      before_validation :set_issue_id
    
      private
        def set_issue_id
          self.issue_id = self.company.issues.size + 1
        end
    end
    
    class Company < ActiveRecord::Base
      has_many :issues
    end
    

    and inside a migration:

    add_index :issues, [:issue_id, :company_id], :unique => true
    

    And you could grab the correct issue in the controller like you said:

    @issue = Issue.where(company_id: current_user.company.id, issue_id: params[:id])
    

    Note that this doesn’t provide a way to recover from a constraint violation exception in case that actually happened. See @PinnyM answer for a suggestion on how to handle this.

    Hope this helps.

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

Sidebar

Related Questions

Considering the following string, I would like to get a substring in that way:
I'm considering the following: I have some data stream which I'd like to protect
I have the following doubt. I am considering the option to have html tags
Considering that I have a Schema named SBST I want to find all empty
Considering the following data example : data ={ {a, b, c, d, e}, {1,
Considering the following example (I posted it for several different questions today) : #include
Considering the following : http://jsfiddle.net/Johnny5/EC8kK/ I would like the input button to align right
I have recently switched from python 2.7 to python 3.2 considering following folder structure:
Considering the following javascript example: var myobj = { func1: function() { alert(name in
Considering the following example, the line int a = objT + 5; gives ambiguous

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.