簡體   English   中英

rails 3.1 拐點問題

[英]rails 3.1 inflection problem

我有一個帶有以下 2 個模型的 Rails 3.1 應用程序

class Listing < ActiveRecord::Base
  has_many :listing_saves
end

class Team < ActiveRecord::Base
  has_many :listing_saves
  has_many :saved_listings, through: :listing_saves, source: 'listing'
end

Join model 看起來像這樣

class ListingSave < ActiveRecord::Base
  belongs_to :team
  belongs_to :listing
end

Mow 我認為存在拐點問題,因為每當我嘗試運行我的測試時,我都會收到以下錯誤(這是一個錯誤示例和導致它的測試)

it "should return the listing saves associated with the team" do
  save = Factory :listing_save, listing: @listing, saver: @user, team: @team
  @team.listing_saves.should include save
end

Failures:

  1) Team listing_saves associations should return the listing saves associated with the team
     Failure/Error: @team.listing_saves.should include save
     NameError:
       uninitialized constant Team::ListingSafe
     # ./spec/models/team_spec.rb:55:in `block (3 levels) in <top (required)>'

好像 Rails 正在將listing_safe單數化為listing_saves

這是我嘗試過的一些自定義變形器(不是同時嘗試的)(它們都不起作用)

# config/initializers/inflections.rb
ActiveSupport::Inflector.inflections do |inflect|
  inflect.plural 'saved_listing', 'saved_listings'
  inflect.singular 'saved_listings', 'saved_listing'
  inflect.plural 'listing_save', 'listing_saves'
  inflect.singular 'listing_saves', 'listing_save'
  inflect.singular 'listing_safes', 'listing_safe'
  inflect.plural 'listing_safe', 'listing_safes'
  inflect.irregular 'listing_save', 'listing_saves'
  inflect.irregular 'saved_listing', 'saved_listings'
end

接下來我能做什么?

注意:我發現了這個類似的問題,但答案似乎沒有解決我的問題

編輯我遵循下面的答案,所以我現在在我的config/initializers/inflections.rb中有以下內容

ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'listing_save', 'listing_saves'
end

當我打開控制台 session 並運行"listing saves".singularize我希望得到“listing_save”。 但是,似乎我的應用程序至少有一部分沒有得到它,我的測試仍然像以前一樣失敗。 (我發誓在測試/運行應用程序之前我會重新啟動我的服務器和 spork。)。

編輯 2我為我的應用程序中的變形寫了一些測試:

describe "inflection" do
  it "should singularize listing_saves properly" do
    "listing_saves".singularize.should == "listing_save"
  end

  it "should pluralize listing_save properly" do
    "listing_save".pluralize.should == "listing_saves"
  end
end

現在我遇到了這些測試通過正常的情況,但其他測試仍然失敗,並出現與我之前相同的錯誤

NameError:
       uninitialized constant User::ListingSafe

相同的應用程序,相同的 spork 實例,加載相同的文件。 這里發生了什么奇怪的事情???

您需要定義一個不規則拐點:

# Test your inflections!
> "listing_save".pluralize
=> "listing_saves" # OK!
> "listing_saves".singularize
=> "listing_safe"  # Ouch :(

# Make it smarter
ActiveSupport::Inflector.inflections { |i| 
  i.irregular 'listing_save', 'listing_saves' 
}

# Test again
> "listing_saves".singularize
=> "listing_save"  # Yay!

Ruby 文檔:

------------------------ ActiveSupport::Inflector::Inflections#irregular
     irregular(singular, plural)
------------------------------------------------------------------------
     Specifies a new irregular that applies to both pluralization and
     singularization at the same time. This can only be used for
     strings, not regular expressions. You simply pass the irregular in
     singular and plural form.

     Examples:

       irregular 'octopus', 'octopi'
       irregular 'person', 'people'

編輯:

一些進一步的調查 - 看起來其他人也偶然發現了同樣的問題(變形與關聯沒有按預期工作)。 因此,與此同時,您可以手動設置 class 名稱:

has_many :listing_saves, :class_name => "ListingSave"

其他人有同樣的問題,並進行了額外的屈折調整。 我個人會使用:class_name設置來代替 go :

Rails 3.0.3 上 Ruby 中的自定義變形問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM