我有2个型号,Microspost并且User:
class Micropost < ActiveRecord::Base
belongs_to :user
default_scope -> { order(created_at: :desc) }
validates :user_id, presence: true
validates :content, presence: true, length: { maximum: 140 }
end
class User < ActiveRecord::Base
has_many :microposts, dependent: :destroy
attr_accessor :remember_token, :activation_token, :reset_token
before_save :downcase_email
before_create :create_activation_digest
validates :name, presence: true, length: { maximum: 50 }
end
Run Code Online (Sandbox Code Playgroud)
seed.rb :
User.create!(name: "Example User",
email: "example@railstutorial.org",
password: "foobar",
password_confirmation: "foobar",
admin: true,
activated: true,
activated_at: Time.zone.now)
99.times do |n|
name = Faker::Name.name …Run Code Online (Sandbox Code Playgroud) 我有一个这样的 CSV:
1,"Paris","3.57"
10,"Singapore","3.57"
211,"Sydney","3.28"
324,"Toronto Center","3.33"
Run Code Online (Sandbox Code Playgroud)
我想用零填充第一列以获得:
001,"Paris","3.57"
010,"Singapore","3.57"
211,"Sydney","3.28"
324,"Toronto Center","3.33"
Run Code Online (Sandbox Code Playgroud)
printf我尝试将第一列分配给awk的输出:
awk '{ $1 = printf("%03d", $1); print }' my.csv
Run Code Online (Sandbox Code Playgroud)
但它给了我一个语法错误:
awk: cmd. line:1: { $1 = printf("%03d", $1); print }
awk: cmd. line:1: ^ syntax error
Run Code Online (Sandbox Code Playgroud)
如果我引用 printf 函数,它也不起作用。
我怎么能这么做呢?