0

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?

holyredbeard
  • 19,619
  • 32
  • 105
  • 171
  • 1
    try `pu5 = ProjectsUsers.create(:user_id => 12, :project_id => 6)`. But anyway its not a good idea as I mentioned in other question. You should use `has_many :through` for such cases. for reference go through http://guides.rubyonrails.org/association_basics.html#choosing-between-has_many-through-and-has_and_belongs_to_many . This will help you understand the difference. :) – Manoj Monga Feb 10 '13 at 18:05

1 Answers1

1

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)
Andrei
  • 1,121
  • 8
  • 19
  • Thanks a lot. I changed the name to Projects_user as you suggested, but now I get the following error instead: "uninitialized constant ProjectsController::Projects_user". Do you know what that means? – holyredbeard Feb 11 '13 at 20:27
  • Please post the contents of your projects_controller file. Anywhere you might have `Projects_user` - should say `ProjectsUser` – Andrei Feb 11 '13 at 20:34
  • I removed the underscore and that did the trick! Thanks a lot! – holyredbeard Feb 11 '13 at 21:11