3

다음 2 개의 모델이있는 Rails 3.1 응용 프로그램이 있습니다.레일 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 

결합 모델은 다음과 같습니다.

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)>' 

레일즈가 listing_safelisting_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 

다음에 무엇을 할 수 있습니까?

참고 : this similar question를 찾았지만 대답이 내 문제를 해결하지 못하는 것 같습니다.

편집 config/initializers/inflections.rb

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

콘솔 세션을 열고 "listing saves".singularize을 실행하면 "listing_save"라는 메시지가 나타납니다. 그러나, 적어도 내 응용 프로그램의 일부가 그것을 얻지 않는 것, 내 테스트는 이전과 같은 방식으로 실패합니다. (응용 프로그램을 테스트/실행하기 전에 서버와 스포크를 다시 시작한다고 맹세합니다!).

편집 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 인스턴스, 같은 파일이로드되었습니다. 이상한 일이 벌어지고있어! "

+0

아래 나의 새로운 코멘트를 참조하십시오. – Casper

+0

어쩌면 어리석은 질문 일지 모르겠지만 ... 만약 당신에게 이런 많은 문제가 생기면 - 다른 이름을 찾지 못할 이유가 있을까요? ;) –

+0

물론, 하하 물론 우리 둘 다 내가 이길 수는 없다는 것을 알고 있습니다! –

답변

11

당신은 불규칙한 활용을 정의 할 필요가

------------------------ 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' 

편집 :

일부 추가 조사 - 그것은이 우연히 다른 사람들처럼 보인다 같은 문제도 (협회와 함께 예상대로 작동하지 않는 굴절). 같은 문제를 가진 다른

has_many :listing_saves, :class_name => "ListingSave" 

사람, 그리고 추가 억양은 조정할 : 그래서 그 동안 수동으로 클래스 이름을 설정할 수 있습니다.개인적으로 나는 비록 대신 설정 :class_name와 함께 가고 싶어요 :

Issue with custom inflections in Ruby on Rails 3.0.3

+0

위대한 답변, 고마워! –

+0

안녕하십니까, 수락을 취소 한 것에 대해 사과드립니다. 오늘까지 부분적으로 만 솔루션을 테스트했습니다. 이상하게도, 그것은 콘솔에서 작동하지만 테스트 나 어플리케이션을 실행할 때 작동하지 않습니다. 새로운 상황으로 OP를 편집했습니다. –

+0

안녕하세요. 문제가 없습니다. spork이 inflections 파일을로드하지 않는 것 같습니다. 'spork -d'를 사용하여 spork을 실행하여로드되는 내용을보고, 또는 inflections 파일을 'spec_helpers.rb'의 spork preload 섹션에 추가하십시오. 또한 억양 만 테스트하는 테스트 케이스를 만드십시오. 당신이 필요로하는 것처럼 보입니다! :) – Casper