我正在播种一些帖子(seeds.rb)。但是我想在Faker中本地添加一个方法(past_week)。我收到了一个错误
seeds.rb
Post.create(
:title => Faker::Lorem.words(4),
:content => Faker::Lorem.paragraph(2)
:created_at => Faker::Date.past_week
)
faker.rb(在我的〜/ .rvm / gems / ruby-2.1.0 / faker1-3-0
require 'time'
require 'date'
require 'faker/date'
在我的date.rb中(在我的〜/ .rvm / gems / ruby-2.1.0 / faker1-3-0 / lib
module Faker
class Date < Base
class << self
def past_week
#return a random day in the past 7 days
today = Date.today
today = today.downto(today - 7).to_a
today.shuffle[0]
end
end
end
end
我的错误
NoMethodError: undefined method `today' for Faker::Date:Class
/home/userlaptop/.rvm/gems/ruby-2.1.0/gems/faker-1.3.0/lib/faker.rb:138:in `method_missing'
/home/userlaptop/.rvm/gems/ruby-2.1.0/gems/faker-1.3.0/lib/faker/date.rb:5:in `past_week'
/home/userlaptop/development/public/project/jed/db/seeds.rb:21:in `<top (required)>'
答案 0 :(得分:1)
因为您已将您的班级命名为Date
,所以找不到today
,因为您没有定义该方法。为了引用ruby Date
类,在类前面加上作用域解析运算符:
module Faker
class Date < Base
class << self
def past_week
#return a random day in the past 7 days
today = ::Date.today
today = today.downto(today - 7).to_a
today.shuffle[0]
end
end
end
end