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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:28:49+00:00 2026-05-28T15:28:49+00:00

I have a model Store with attributes user_id, product_id and token_string . In the

  • 0

I have a model Store with attributes user_id, product_id and token_string.

In the form I collect values for user_id and product_id but not token_string because it is going to be generated after user_id and product_id are saved.
So in the create action the @store is saved normally with only two attributes values and nil token_string.

But right after @store is saved i want to generate a random token_string(doesn’t matter how) and save it to the database on the same row as product_id and user_id where there is nil(i am not sure if update_attribute would do).But the biggest challenge is there is a number from a variable i have to multiply to token generation.

So lets say the number is 2
That means i would generate two token_strings and both have to be saved to the same user_id and product_id.How do i go around this?

Thank you in advance

    def create
      @quantity=2
      @store=Store.new(params[:store])

      if @store.save
         @quantity.times{ generate token string}
         #Then save both generated tokens to the same user_id and product_id
        redirect_to :controller=>"products",:action=>"index"
      else
        redirect_to :action=>"new"
      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-28T15:28:51+00:00Added an answer on May 28, 2026 at 3:28 pm

    if you need to save token_string in the same record

    class Store < ActiveRecord::Base
    
      before_save :token_generate
    
      private
    
      def token_generate
        self.token_string = Digest::SHA1.hexdigest(user_id + token_id + Time.now)
      end
    
    end
    

    but if you need to duplicate the @store with the same user_id and product_id that method can be helpful

    # model
    class Store < ActiveRecord::Base
      def duplicate_with_token!(quantity)
        1.upto(quantity) do
          obj = self.clone
          obj.token_string = Digest::SHA1.hexdigest(user_id + token_id + Time.now)
          obj.save
        end
      end
    end
    
    # controller
    class StoresController < ApplicationController
      def create
        @store=Store.new(params[:store])
        if @store.save
          @store.duplicate_with_token!(2)
          #Then save both generated tokens to the same user_id and product_id
          redirect_to "products#index"
        else
          redirect_to :new
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following model set up: class UserProfile(models.Model): Additional attributes for users. url
I have a User model with the usual attributes such as email and hashed_password
I have a Rails model with various attributes and has_many relationships to other models.
I have a number of models that store words as a string with each
i have an entity-model file (edmx) file which contains few tables and few stored
I have a class in my domain model called JobPlan this class is stored
Let's say I have a RESTful, hypertext-driven service that models an ice cream store.
I have model Foo which has field bar. The bar field should be unique,
I have model Article it has field title with some text that may contain
Lets say I have model inheritance set up in the way defined below. class

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.