开发者

Using Friendly ID and i18n with update_attributes causes overwrite

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 prob开发者_高级运维lem?

Code: Model:

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


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.


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".

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜