Heroku上的Rails Friendly_Id,Heroku没有更新slu。 [英] Rails Friendly_Id on Heroku, Heroku not updating slugs

查看:121
本文介绍了Heroku上的Rails Friendly_Id,Heroku没有更新slu。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了 friendly_id 宝石,以下是 RailsCasts Screencast 就可以了。本地这工作出色,我安装它,跑过 User.find_each(&保存)成功更新每个现有用户的slug字段,一切都很好。

现在我已经把它推到了Heroku中,尽管它对于添加的任何新成员都可以正常工作,Heroku并没有为已经存在的用户更新我的用户slug 。



运行 heroku run console 然后 User.find_each(&:save)



  irb(main):001:0> User.find_each(& $ save)
User Load(20.2ms)SELECTusers。* FROMusersWHERE(users。id> = 0)ORDER BYusers。 idASC LIMIT 1000
(23.6ms)BEGIN
(2.0ms)COMMIT
(13.6ms)BEGIN
(21.1ms)COMMIT
=>无

models / user.rb 是:

  extend FriendlyId 
friendly_id:name,use::slugged
def should_generate_new_friendly_id?
new_record?
end

我已经在Heroku上运行了所有数据库迁移,并且它们都很好。我检查了用户表的确有一个 slug 字段,但它是空的,我不确定我在哪里,这里错了。如果有人碰巧遇到过这种情况,任何建议将不胜感激。谢谢。

解决方案

当然,这总是很傻。我真的不知道我是怎么错过的:



这段代码:

  def should_generate_new_friendly_id? 
new_record?
end

如果名称被更改,还停止更新以前的帖子。



我解决了这个问题,通过评论这个问题,推动,运行Heroku控制台&然后 User.find_each(&:save)工作得很好,然后我没有评论代码&再次推到Heroku,一切都很华丽。



希望这可以帮助未来的人!


I've set up the friendly_id gem, following along to the RailsCasts Screencast on it. Locally this works brilliantly, I installed it, ran through User.find_each(&:save) which successfully updated the slug field on each of the existing users and all was fine.

I have now pushed this to Heroku, and although it works fine for any new members that are added, Heroku isn't updating the user slug for me for already existing Users.

Running heroku run console and then User.find_each(&:save) just does nothing:

irb(main):001:0> User.find_each(&:save)
  User Load (20.2ms)  SELECT "users".* FROM "users" WHERE ("users"."id" >= 0) ORDER BY "users"."id" ASC LIMIT 1000
    (23.6ms)  BEGIN
    (2.0ms)  COMMIT
    (13.6ms)  BEGIN
    (21.1ms)  COMMIT
=> nil

The relevant code in models/user.rb is:

extend FriendlyId
friendly_id :name, use: :slugged
def should_generate_new_friendly_id?
  new_record?
end

And I've run all DB migrations on Heroku and they went fine. I checked and the users table does have a slug field, but it's empty and I'm not sure where I'm going wrong here. If anyone happens to have come across this before, any advice would be much appreciated. Thank you.

解决方案

And of course, it's always something silly. I'm really not sure how I missed this:

This piece of code:

def should_generate_new_friendly_id?
  new_record?
end

Which is there to stop new slugs being made if the name is changed, was also stopping it updating previous posts.

I solved the issue by commenting this out, pushing, running the Heroku console & then User.find_each(&:save) worked just fine, I then un-commented the code & pushed to Heroku again, and everything is dandy.

Hopefully this may help someone in the future!

这篇关于Heroku上的Rails Friendly_Id,Heroku没有更新slu。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆