2013-08-26 24 views
1
class User < ActiveRecord::Base 
    has_many :ties, dependent: :destroy 
    has_many :albums, through: :ties 
end 

class Album < ActiveRecord::Base 
    has_many :ties, dependent: :destroy 
    has_many :users, through: :ties 
end 

class Tie < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :album, dependent: :destroy 
end 

ķ......所以,當試圖創建相冊,從AlbumsController#創建行動:NameError與has_many_through表

def create 
    @album = current_user.albums.build(params[:album]) #error is on this line 
    if @album.save 
    flash[:success] = "#{@album.description} created!" 
    redirect_to @album 
    else 
    flash[:error] = 'Looks like something was invalid with that album. Try again.' 
    redirect_to albums_path 
    end 
end 

我越來越uninitialized constant User::Ty。我認爲鐵軌TieTy混淆。任何想法?我可以強制從en.yml某些名字?

回答

3

這的確是因爲Rails試圖單數化ties來派生類名。最好的解決辦法是爲此定義一個新的變形規則。在Rails 4,你這樣做:

ActiveSupport::Inflector.inflections(:en) do |inflect| 
    inflect.singular /^ties$/i, 'tie' 
end 

但在Rails 3中你可以這樣做:

ActiveSupport::Inflector.inflections do |inflect| 
    inflect.singular /^ties$/i, 'tie' 
end 
+0

是有一些原因,你不建議使用'irregular',瑞安?這是否僅適用於默認規則不適用於任何方向的情況? –

+0

@PeterAlfvin:是的,沒錯。 「領帶」的單數將是「領帶」,但如果你將「領帶」複數化,你總會得到「領帶」。 –

+0

'C:/railsinstaller/ruby193/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/inflector/inflections.rb:166:in'inflections':錯誤的參數個數(1爲0)(ArgumentError)'當試圖在那之後啓動服務器時。 – Dudo