Question

What I'm trying to do is to create posts in a relational table called 'projects_users' with the seed, but after a 'rake db:seed' the following error is thrown:

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

projects_users.rb:

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

('projects_users' has a controller and view too (also not used))

projects_users table:

project_id
user_id

projects model:

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

users model:

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)

What does the error mean and how do I fix this?

Était-ce utile?

La solution

Your model must be singular. You need to rename your model from projects_users.rb to projects_user.rb Also the class definition should say:

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

So in your seeds file you should be making a call to it like this:

pu5 = ProjectsUser.create(:user_id => 12, :project_id => 6)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top