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

The Archive Base Latest Questions

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

I have a model Token with three fields user_id,product_id and unique_token .In the controller

  • 0

I have a model Token with three fields user_id,product_id and unique_token.In the controller i instantiate a @token object with user_id and product_id values collected from the form.Then i call save_with_payment function with that object,where within the function i want to generate random string 3 times and save in unique_token field.The problem is self.tokens.create!( unique_token: Digest::SHA1.hexdigest("random string") ) give me no method error undefined method tokens.What am i doing wrong here?To clarify what i want to accomplish,I want to be able to retrieve list of generated unique_tokens associated to that user_id or product_id like User.find(1).tokens or Product.find(1).tokens.The model association is User has_many Tokens Product has_many Tokens.Note: unique_token field is from Token model originally,user_id and product_id are just ref primary keys.Much Thanks!

def create
  @token=Token.new(params[:token])
  if @token.save_with_payment
    redirect_to :controller => "products", :action => "index"
  else
    redirect_to :action => "new"
  end
end

class Token < ActiveRecord::Base
  require 'digest/sha1'

  def save_with_payment
 #  if valid?
 #   customer = Stripe::Charge.create(amount:buck,:currency => "usd",card:stripe_card_token,:description => "Charge for bucks")
#self.stripe_customer_token = customer.id
    3.times do
      self.tokens.create!(unique_token: Digest::SHA1.hexdigest("random string"))
    end 
    save!
  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:29:30+00:00Added an answer on May 28, 2026 at 3:29 pm

    There is no tokens method on the Token class. Since you’re creating three tokens you don’t need the @token instance. Just have save_with_payment be a class method:

    def create
      if Token.save_with_payment(params[:token])
        redirect_to :controller => "products", :action => "index"
      else
        redirect_to :action => "new"
      end
    end
    
    class Token < ActiveRecord::Base
      require 'digest/sha1'
    
      def self.save_with_payment(attributes)
        attributes.merge!(unique_token: Digest::SHA1.hexdigest("foo"))
        3.times do
          self.create!(attributes)
        end
      end
    
    end
    

    Hope this helps.

    You might want to wrap the loop in a begin/rescue, too. Otherwise if the 2nd or 3 create! fails you end up with tokens AND redirecting to “new”.

    Response to 1st comment:
    That won’t work if you use a class method. You can’t call valid? because you’re not in the context of an instance of Token. I don’t recommend sticking with an instance method. If you do change it to a class method you’ll want to wrap it in a transaction block:

    def self.save_with_payment(attributes)
      transaction do
        attributes.merge!(unique_token: Digest::SHA1.hexdigest("foo"))
        3.times do
          self.create!(attributes)
        end
      rescue
        false
      end
    end
    

    That should roll back the SQL transactions if any of the create! calls fail and return false to the controller create action.

    I’d pull that customer code out of Token (Token shouldn’t care about creating/retrieving a customer) and put it in the controller action. Pass the pertinent information into save_with_payments. Like:

    self.save_with_payments(customer, attributes)
      ...
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model Store with attributes user_id, product_id and token_string . In the
If I have Model.objects.all() I want to get only one object for any content_object=foo,
In my controller I have model operations that can return empty results. I've setup
I have a model Model that can be access from many ways: by subdomain
tl;dr: Is it possible to intercept posted values from a nested model for further
I have a model with an object that contains a collection like this: namespace
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
I am newbie at Django. I have model with a custom method. In view

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.