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

  • Home
  • SEARCH
  • 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 5839791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:38:19+00:00 2026-05-22T11:38:19+00:00

With the help of some here, I’m using this code to generate tokens purchased

  • 0

With the help of some here, I’m using this code to generate tokens purchased from my app. It loops through depending on how many tokens are ordered, calls a method called create_trackable_token which just generates a unique string to identify the token. I have two questions about this.

1) How can I rewrite it so the particular columns are not vulnerable to mass-assignment? Right now, if I use ‘attr_accessible` on this model, I have to expose the three attributes in this method because it’s assigning them at once. I’d prefer to not do that. The method is already protected, so it can’t be called by an end user.

2) What’s the best way of handling errors? The tokens have to be unique, and right now, I’m not sure what would happen if the create_trackable_token method generates a string that’s already in use. Does ActiveRecord take care of that or do I need to write some error handling into the method?

protected
    def create_trackables
       return unless self.success
       order = Order.find(order_id) #you shouldn't need this line if it has_one :order
       1.upto(order.total_tokens) do
         Tracker.create!(
                        :user_id => order.user_id,
                        :token => Tracker.create_trackable_token,
                        :order_id => order_id
                        )
       end
     end
  • 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-22T11:38:20+00:00Added an answer on May 22, 2026 at 11:38 am

    To handle uniqueness, use a validation on the column, see http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#method-i-validates_uniqueness_of

    When creating the token, there are many things to consider, such as how hard do you want it to be to guess (or else do you authenticate a token against something else?) … you could use hashes, such ass MD5, random number, or you could go all out and make a GUID. In any case, you could look for the token before returning from create_trackable_token.

      Tracker.where(:token => generated_token).exists?
    

    If you want to keep it mass assignment safe you have to do this in a few steps:

    t = Tracker.create(
                   :user_id => order.user_id,
                   :order_id => order_id
                )
    t.token = Tracker.create_trackable_token
    t.save!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've gotten some help here for this piece of code, but on my end,
Well i need some help here i don't know how to solve this problem.
hope to get some help here because this is something that really makes me
I need some help extracting the following bits of information using regular expressions. Here
I have a piece of code here that i really could use some help
Hopefully I'll get some help here. Basic setup is this: My application swaps out
After some help here, I've got WPF using the windows.forms notifyIcon class (It's not
I need some help here. Can't seem to get a way around this. Basically
need help from some gurus here, I have a database which I have moved
Guys i need some help here , im getting this error here at the

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.