The table between the User
model and Period
model does not exist. They have a has_many_and_belongs_to relationship. I'm learning Rails still, so please bear with me.
This is the contents of the User.rb model file
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
has_many :assignments, :order => "end"
has_and_belongs_to_many :periods
end
This is the contents of the Period.rb model file
class Period < ActiveRecord::Base
has_many :weights
has_many :assignments
has_and_belongs_to_many :users
end
The exact error message is Could not find table 'periods_users'
. This occurs when an instance of Period or User tries to invoke users or periods (i.e new_user.periods
or new_period.users
)
I'm using SQLite 3 for the development database, and SQLite 3 is properly installed.
I presumed that Rails would create the intermediary tables for habtm relationships. Do I have to create them myself, and if so, how?
(Please note that the other models associations are working just fine)
If I'm not giving enough information please let me know.
edit: After trying Method B, it threw:
undefined method "klass" for nil:NilClass
This occurred while trying to manage the User
and Period
models in rails_admin
So, I deleted the new model, and tried Method A. It threw
undefined method "period_id" for <ActiveRecord::ConnectionAdapters::SQlite3Adapter:0x3df7760>
while trying to run
rake db:migrate
Here is the migration:
class CreateTablePeriodsUsers < ActiveRecord::Migration
def self.up
create_table 'periods_users' do |t|
t.integer period_id
t.integer user_id
end
end
def self.down
drop_table 'periods_users'
end
end
edit #2
Now it's having a "create_has_and_belongs_to_many_reflection: Primary Key is not allowed in a has_and_belongs_to_many join table"
This occurred after trying to run "rails server", and before that I ran rake db:migrate
successfully
edit #3:
I fixed this a little less than a week ago, it ended up being an issue with the database schema, so I just dropped the database and re-migrated and it worked fine.