Ruby on rails 在Rails中,如何使用字母“N”作为id前缀并另存为项目编号
我需要能够根据产品id创建一个项目编号,前缀为字母N。我已经看到了关于和使用的这两个问题 我对RubyonRails和一般编程都是新手。如何将这两个问题结合起来,以便保存后,我的项目编号字段将用新连接的K+id更新 我正在使用Ruby 2.3和Rails 4.2.5 更新-最终解决方案 多亏了@born4new,我在模型中使用的最终代码如下所示Ruby on rails 在Rails中,如何使用字母“N”作为id前缀并另存为项目编号,ruby-on-rails,string-concatenation,Ruby On Rails,String Concatenation,我需要能够根据产品id创建一个项目编号,前缀为字母N。我已经看到了关于和使用的这两个问题 我对RubyonRails和一般编程都是新手。如何将这两个问题结合起来,以便保存后,我的项目编号字段将用新连接的K+id更新 我正在使用Ruby 2.3和Rails 4.2.5 更新-最终解决方案 多亏了@born4new,我在模型中使用的最终代码如下所示 class Product < ActiveRecord::Base after_save :create_item_number pr
class Product < ActiveRecord::Base
after_save :create_item_number
private
# Concatenate ID with prefix N to create item number
def create_item_number
update_column(:item_number, self.item_number = "N#{id}")
end
end
您可以执行如下简单的字符串连接:
after_save :prefix_number
[...]
private
def prefix_number
self.number = "N#{id}"
end
非常感谢@born4new,我将用我用于模型的最终代码更新这个问题。