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

K...所以,当尝试从 AlbumsController#Create 操作创建专辑时:

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。我认为 railsTieTy. 任何想法?我可以强制使用某些名称en.yml吗?

4

1 回答 1

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
于 2013-08-26T22:33:03.820 回答