开发者

Why isnt' this multiple field key working in Mongoid?

I've added this to my model:

key :name, :random_number

And I am using this callback:

before_create :create_random_number

But random_number is not getting appended to the _id using a method like this:

def create_random_number
   rand(9999开发者_JAVA技巧9999999999999999)
end

This is the result that I get:

>> Product.create(name: "foo")
   => <Product _id: foo,


It turns out that you need to use after_initialize. This works for me:

key :slug
after_initialize :create_slug

def create_slug
  name = self.name.gsub(' ', '-')
  self.slug = "#{name}-#{rand(36**20).to_s(36)}"
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜