我想做的是在一个名为"projects_users"的关系表中创建带有种子的post,但在"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
种子.rb:
pu5 = Projects_users.create(:user_id => 12, :project_id => 6)
这个错误意味着什么?我该如何解决这个问题?
您的模型必须是单数。您需要将模型从projects_users.rb重命名为projects_user.rb此外,类定义应该说:
class ProjectsUser < ActiveRecord::Base
// no code yet
end
所以在你的种子文件中,你应该这样打电话:
pu5 = ProjectsUser.create(:user_id => 12, :project_id => 6)