0

我想要做的是在一个名为“projects_users”的关系表中使用种子创建帖子,但是在“rake db:seed”之后会引发以下错误:

Expected /...path.../projects_users.rb to define Projects_users

项目_用户.rb:

class ProjectsUsers < ActiveRecord::Base
    // no code yet
end

(“projects_users”也有一个控制器和视图(也没有使用))

projects_users 表:

project_id
user_id

项目型号:

class Project < ActiveRecord::Base
  has_and_belongs_to_many :users, :class_name => 'User'
  belongs_to :user

  has_many :tickets, :dependent => :destroy

  attr_accessible :user_id, :title, :description, :start_date, :end_date
end

用户型号:

class User < ActiveRecord::Base
  attr_accessible :first_name, :last_name, :email, :password

  has_and_belongs_to_many :projects
  has_many :tickets

  before_save :create_remember_token

  def create_remember_token
    self.remember_token = SecureRandom.urlsafe_base64
  end

end

种子.rb:

pu5 = Projects_users.create(:user_id => 12, :project_id => 6)

错误是什么意思,我该如何解决?

4

1 回答 1

1

您的模型必须是单数的。您需要将模型从projects_users.rb重命名为projects_user.rb 类定义也应该说:

class ProjectsUser < ActiveRecord::Base
  // no code yet
end

因此,在您的种子文件中,您应该像这样调用它:

pu5 = ProjectsUser.create(:user_id => 12, :project_id => 6)
于 2013-02-10T18:48:18.743 回答