将友好 ID 和 i18n 与 update_attributes 一起使用会导致覆盖

发布于 2024-12-05 14:20:18 字数 876 浏览 1 评论 0原文

使用: - 轨道 3.0.3 -Friendly_id 4.0.0 测试版 11 - 窗户 - i18n (0.6.0, 0.5.0) 根据“gem list”

设置: 我有一个使用瑞典语和英语作为语言进行计算的多语言网站。我正在使用Friendly_id 来创建SEO 友好的URL。

在我的 mysql 表中,我有“slug”、“slug_se”和“slug_en”列。我不确定是否需要使用“slug”列,它与“slug_en”相同。

问题: 使用“update_attributes”时,它会用“slug”覆盖“slug_se”(默认情况下为英语)。因此,“slug”中的值被写入“slug_se”中。

故障排除: - 我在 Google 上搜索了这个问题,只在 Google 论坛 (http://groups.google.com/group/friend_id/browse_thread/thread/154f4a5024e23418) 上找到了一个(未答复的)论坛主题,该主题尚未得到答复。 - 我试图找到 update_attributes 的替代方案,我可以通过 mysql,但这确实不太好。

问题: - 我是否需要使用“slug”列? - 是否有 update_attributes 的替代方案,也不会尝试更新 id? - 你能看出我所做的事情有什么问题导致了这个问题吗?

代码: 模型:

  extend FriendlyId
  friendly_id :name, :use => :slugged, :use => I18n

Using:
- Rails 3.0.3
- Friendly_id 4.0.0 Beta 11
- Windows
- i18n (0.6.0, 0.5.0) according to "gem list"

Set-up:
I have a multilingual website about calculations using Swedish and English as languages. I am using friendly_id to create seo-friendly url's.

In my mysql-table I have columns "slug", "slug_se" and "slug_en". I am not sure if I need to use the "slug"-column, it is identical with "slug_en".

Problem:
While using "update_attributes" it overwrites "slug_se" with "slug" (which by default is in English). Thus, the value in "slug" is written in "slug_se".

Trouble shooting:
- I have Googled this issue and only found one (unanswered) forum thread on Google Forum (http://groups.google.com/group/friendly_id/browse_thread/thread/154f4a5024e23418) that was left unanswered.
- I have tried to find alternatives to update_attributes, I could go through mysql but that really wouldn't be neat.

Questions:
- Do I need to use the "slug"-column at all?
- Is there an alternative to update_attributes that does not try to update the id as well?
- Can you see anything wrong in what I am doing that is causing the problem?

Code:
Model:

  extend FriendlyId
  friendly_id :name, :use => :slugged, :use => I18n

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情绪操控生活 2024-12-12 14:20:18

我是FriendlyId 的作者。你可能会更幸运地在该项目的 Github 问题中提出这个问题,我只是偶然看到这个问题。

i18n 模块仍然很新,因此对不稳定感到抱歉并感谢您的反馈。 :)

在回答您关于 slug 列的问题时,不,您根本不应该拥有它:只需 slug_en 和 slug_se 。这是 9 月 4 日进行的更改:

https://github.com/norman/Friendly_id/commit/54536464132ac8f72c96e8bda203c337f9d56aa0

所以尝试删除该列。如果您的问题仍然存在,请随时在 Github 上发布错误报告,我会尽快查看。

同时,我将尝试添加一个测试来尝试重现您描述的问题,如果我可以重现它,我一定会修复它。

I'm the author of FriendlyId. You'd probably have better luck asking this question in the Github issues for the project, I just happened to see this by chance.

The i18n module is still pretty new, so sorry for the instability and thanks for the feedback. :)

In answer to your question about the slug column, no you should not have it at all: just slug_en and slug_se. This was a change made on September 4th:

https://github.com/norman/friendly_id/commit/54536464132ac8f72c96e8bda203c337f9d56aa0

So try removing that column. If your problems persist please feel free to post a bug report on Github and I'll look at it as soon as I can.

In the mean time I will try adding a test to try to reproduce the issue you describe, and if I can reproduce it, I'll definitely fix it.

饭团 2024-12-12 14:20:18

诺曼,谢谢。我删除了 slug 列,但这并没有解决问题。看起来默认的 slug 会覆盖 update_attributes 处的 slug_se 。

这是来自日志:

    Started POST "/vardag/procent/result" for 127.0.0.1 at 2011-09-22 07:36:02 +0200
  Processing by CalculationsController#result as HTML
  Parameters: {"utf8"=>"Ô£ô", "authenticity_token"=>"Xrq9Zf8jGl2X1G9WvqMpcN1EQw7C2lda561FN9E7ZdM=", "first_number"=>"1.0", "second_number"=>
"2.5", "operation"=>"from_a_to_b", "commit"=>"Beräkna", "calculation_type_id"=>"vardag", "id"=>"procent"}
  ←[1m←[36mCalculation Load (1.0ms)←[0m  ←[1mSELECT `calculations`.* FROM `calculations` WHERE (`calculations`.`slug_se` = 'procent') LIMIT
1←[0m
  ←[1m←[35mCalculationType Load (1.0ms)←[0m  SELECT `calculation_types`.* FROM `calculation_types` WHERE (`calculation_types`.`id` = 1) LIMI
T 1
  ←[1m←[36mCACHE (0.0ms)←[0m  ←[1mSELECT `calculations`.* FROM `calculations` WHERE (`calculations`.`slug_se` = 'procent') LIMIT 1←[0m
  ←[1m←[35mSQL (1.0ms)←[0m  BEGIN
  ←[1m←[36mCalculation Load (1.0ms)←[0m  ←[1mSELECT `calculations`.* FROM `calculations` WHERE (`slug_se` = 'percentage' OR `slug_se` LIKE '
percentage--%') AND (id <> 1) ORDER BY LENGTH(`slug_se`) DESC, `slug_se` DESC LIMIT 1←[0m
  ←[1m←[35mAREL (1.0ms)←[0m  UPDATE `calculations` SET `recently` = 'someone_realized,that,when,1.0,goes_to,2.5,result_percentage_a_to_b_inc
reased,150.0,%', `slug_se` = 'percentage', `updated_at` = '2011-09-22 05:36:03' WHERE (`calculations`.`id` = 1)

检查@36mCACHE,它(正确地)使用slug_se = procent,然后向下两行@36mCalculation LOAD它进行选择,其中slug_se =百分比(百分比是slug_en)。

我会在 Git 上写一份错误报告!

编辑:我做了一些可能有帮助的调试:

我在模型中使用此方法调用更新:

 def update_recently(calculation, new_recently_string)
    raise calculation.inspect           
    calculation.update_attributes(:recently => new_recently_string.join(","))
  end

该提升创建了这个:

#<Calculation id: 1, name: "Percentage", preimp: nil, url: nil, clicks: 71, clicks_week: 71, is_local: true, comment: nil, nofollow: true, updated: nil, calculation_type_id: 1, created_at: "2011-03-12 07:04:54", updated_at: "2011-09-22 05:53:25", last_checked: nil, calculation_status_id: 2, region_id: 1, source: nil, affiliate: false, examples: nil, recently: "someone_realized,that,when,1.0,goes_to,2.5,result_p...", is_special: false, slug_en: "percentage", slug_se: "procent">

参数是:

{"utf8"=>"✓",
 "_method"=>"put",
 "authenticity_token"=>"Xrq9Zf8jGl2X1G9WvqMpcN1EQw7C2lda561FN9E7ZdM=",
 "first_number"=>"1.0",
 "second_number"=>"2.5",
 "operation"=>"from_a_to_b",
 "commit"=>"Beräkna",
 "calculation_type_id"=>"vardag",
 "id"=>"procent"}

我做了一个提升计算。Friendly_id.inspect,它显示了“procent”。

Norman, thanks. I removed the slug column but this didn't solve it though. It seems like the default slug overwrites the slug_se at update_attributes.

This is from the log:

    Started POST "/vardag/procent/result" for 127.0.0.1 at 2011-09-22 07:36:02 +0200
  Processing by CalculationsController#result as HTML
  Parameters: {"utf8"=>"Ô£ô", "authenticity_token"=>"Xrq9Zf8jGl2X1G9WvqMpcN1EQw7C2lda561FN9E7ZdM=", "first_number"=>"1.0", "second_number"=>
"2.5", "operation"=>"from_a_to_b", "commit"=>"Beräkna", "calculation_type_id"=>"vardag", "id"=>"procent"}
  ←[1m←[36mCalculation Load (1.0ms)←[0m  ←[1mSELECT `calculations`.* FROM `calculations` WHERE (`calculations`.`slug_se` = 'procent') LIMIT
1←[0m
  ←[1m←[35mCalculationType Load (1.0ms)←[0m  SELECT `calculation_types`.* FROM `calculation_types` WHERE (`calculation_types`.`id` = 1) LIMI
T 1
  ←[1m←[36mCACHE (0.0ms)←[0m  ←[1mSELECT `calculations`.* FROM `calculations` WHERE (`calculations`.`slug_se` = 'procent') LIMIT 1←[0m
  ←[1m←[35mSQL (1.0ms)←[0m  BEGIN
  ←[1m←[36mCalculation Load (1.0ms)←[0m  ←[1mSELECT `calculations`.* FROM `calculations` WHERE (`slug_se` = 'percentage' OR `slug_se` LIKE '
percentage--%') AND (id <> 1) ORDER BY LENGTH(`slug_se`) DESC, `slug_se` DESC LIMIT 1←[0m
  ←[1m←[35mAREL (1.0ms)←[0m  UPDATE `calculations` SET `recently` = 'someone_realized,that,when,1.0,goes_to,2.5,result_percentage_a_to_b_inc
reased,150.0,%', `slug_se` = 'percentage', `updated_at` = '2011-09-22 05:36:03' WHERE (`calculations`.`id` = 1)

Check out @36mCACHE where it (correctly) uses slug_se = procent and then two rows down @36mCalculation LOAD it makes a select where slug_se = percentage (percentage being slug_en).

I will write a bug report at Git!

Edit: I did some more debugging that might be of assistance:

I call an update with this method in the model:

 def update_recently(calculation, new_recently_string)
    raise calculation.inspect           
    calculation.update_attributes(:recently => new_recently_string.join(","))
  end

That raise creates this:

#<Calculation id: 1, name: "Percentage", preimp: nil, url: nil, clicks: 71, clicks_week: 71, is_local: true, comment: nil, nofollow: true, updated: nil, calculation_type_id: 1, created_at: "2011-03-12 07:04:54", updated_at: "2011-09-22 05:53:25", last_checked: nil, calculation_status_id: 2, region_id: 1, source: nil, affiliate: false, examples: nil, recently: "someone_realized,that,when,1.0,goes_to,2.5,result_p...", is_special: false, slug_en: "percentage", slug_se: "procent">

the parameters are:

{"utf8"=>"✓",
 "_method"=>"put",
 "authenticity_token"=>"Xrq9Zf8jGl2X1G9WvqMpcN1EQw7C2lda561FN9E7ZdM=",
 "first_number"=>"1.0",
 "second_number"=>"2.5",
 "operation"=>"from_a_to_b",
 "commit"=>"Beräkna",
 "calculation_type_id"=>"vardag",
 "id"=>"procent"}

I did a raise calculation.friendly_id.inspect and it showed "procent".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文