开发者

Can't update data generated with Populator and Faker gems

I used Ryan Bates' populator gem along with the faker gem to populate my development database with 2k users, 10k posts, and 1...2k votes per post. I generated the data successfully but forgot to update a field in my Post model, votes_count. This is a counter_cache of the votes associated with a post.

Now I'm trying to go back and update the database to have an accurate value for each post object. However, this doesn't seem to be working from the "rails console" environment when I use this little method:

def post_updater
  Post.all.each do |p|
    p.votes_count = p.vo开发者_JAVA百科tes.count
    p.save!
  end
end

I call post_updater and get no errors. However, when I type this

a = Post.first
a.votes_count
=> 0

this is true if I use Post.last, as well. I figured this could just be a fluke and maybe there are Post objects with 0 votes (which shouldn't be true anyways, but lets assume...). I can still do this:

a = Post.first
a.votes_count = a.votes.count
a.save!
=> true
a.votes_count
=> 667
Post.find(a.id)
=> #<Post id: 2175, post_content: "Eveniet adipisci doloremque laborum ea sit sequire...",
   user_id: 2180, category_id: 5, created_at: "2010-04-14 20:06:21",
   updated_at: "2011-03-13 22:01:22",votes_count: 0, flags_count: nil, rank: nil>

Can anyone shed some light on what's going on here? I know the populator gem bypasses validation somehow to save large amounts of data quickly. That's about as much sense as I can make of this, though.


The only thing I can think of is an exception at the database level. Maybe your database is expecting another type of data for votes_count or maybe that is supposed to be a calculated value.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜