2014-06-15 66 views
0

我播種一些帖子(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 /寶石/紅寶石2.1.0/faker1-3-0

require 'time' 
require 'date' 
require 'faker/date' 

在我date.rb(在我的〜/ .rvm /寶石/紅寶石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)>' 

回答

1

因爲您已將您的類命名爲Date,因此您沒有找到today,因爲您沒有定義該方法。爲了引用紅寶石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 
+1

坦克大量的線索。我會投票你的答案將在3分鐘。 –