我正在播种一些帖子(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)>'发布于 2014-06-16 01:42:42
因为您已经将类命名为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
endhttps://stackoverflow.com/questions/24232338
复制相似问题