期望/...path.../projects_users.rb定义Projects_users

时间:2013-02-10 17:27:02

标签: ruby-on-rails database ruby-on-rails-3 model seed

我要做的是使用种子在名为'projects_users'的关系表中创建帖子,但在'rake db:seed'之后会抛出以下错误:

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

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

seeds.rb:

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

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

1 个答案:

答案 0 :(得分:1)

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

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

所以在你的种子文件中你应该像这样打电话给它:

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