尝试使用实例化对象调用函数时没有方法错误
我有一个模型令牌,有三个字段user_id,product_id和unique_token 。在控制器中,我实例化了一个@token对象,其中包含从表单中收集的user_id和product_id值 。然后我用该对象调用save_with_payment函数,在我想要生成的函数中随机字符串3次并保存在unique_token字段中 。问题是self.tokens.create!( unique_token: Digest::SHA1.hexdigest("random string") )
给我没有方法错误undefined method tokens
我在这里做错了什么?为了阐明我想要完成的任务,我希望能够检索与user_find或product_id相关联的生成的unique_tokens列表,如User.find(1).tokens
或Product.find(1).tokens
。模型关联是User has_many Tokens
Product has_many Tokens
。 注意:unique_token字段最初来自Token模型,user_id和product_id只是ref主键。非常感谢!
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 "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
令牌类上没有令牌方法。 由于您正在创建三个令牌,因此您不需要@token实例。 只需将save_with_payment设为类方法:
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
希望这可以帮助。
您可能也希望将循环包装在开始/救援中。 否则如果2或3创建! 你最终没有使用令牌并重定向到“新”。
对第一条评论的回复:如果您使用类方法,则无效。 你不能打电话有效吗? 因为你不在Token实例的上下文中。 我不建议坚持使用实例方法。 如果您确实将其更改为类方法,则需要将其包装在事务块中:
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
如果有任何创建,那应该回滚SQL事务! 调用失败并返回false到控制器创建操作。
我将客户代码从令牌中取出(令牌不应该关心创建/检索客户)并将其置于控制器操作中。 将相关信息传递给save_with_payments。 喜欢:
self.save_with_payments(customer, attributes) ... end