Rails没有创建中间表? - 哈布姆的关系

Jar*_*ner 4 sqlite activerecord model has-and-belongs-to-many ruby-on-rails-3

User模型和Period模型之间的表不存在.他们有一个has_many_and_belongs_to关系.我还在学习Rails,所以请耐心等待.

这是User.rb模型文件的内容

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
Run Code Online (Sandbox Code Playgroud)

这是Period.rb模型文件的内容

class Period < ActiveRecord::Base
  has_many :weights
  has_many :assignments
  has_and_belongs_to_many :users
end
Run Code Online (Sandbox Code Playgroud)

确切的错误消息是Could not find table 'periods_users'.当Period或User的实例尝试调用用户或句点(即new_user.periods或new_period.users)时,会发生这种情况

我正在使用SQLite 3作为开发数据库,​​并正确安装了SQLite 3.

我假设Rails会为habtm关系创建中间表.我是否必须自己创建它们,如果是这样,怎么做?

(请注意,其他型号协会工作正常)

如果我没有提供足够的信息,请告诉我.

编辑:尝试方法B后,它抛出:

undefined method "klass" for nil:NilClass
Run Code Online (Sandbox Code Playgroud)

尝试在rails_admin中管理User和Period模型时发生这种情况

所以,我删除了新模型,并尝试了方法A.它扔了

undefined method "period_id" for <ActiveRecord::ConnectionAdapters::SQlite3Adapter:0x3df7760>

在试图跑步的同时

rake db:migrate

这是迁移:

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
Run Code Online (Sandbox Code Playgroud)

编辑#2

现在它有一个 "create_has_and_belongs_to_many_reflection: Primary Key is not allowed in a has_and_belongs_to_many join table"

这是在尝试运行"rails server"之后发生的,在此之前我rake db:migrate成功运行了

编辑#3:

我在不到一周前解决了这个问题,它最终成了数据库架构的一个问题,所以我只是删除了数据库并重新迁移,它工作正常.

bas*_*man 5

你必须自己创建它们,有两种方法可以做到这一点.

a)只需使用新表创建迁移即可.

b)创建一个用于HABTM关联的新模型(这使得删除关联变得更容易).

方法A:

$ rails generate migration CreateTablePeriodsUsers
Run Code Online (Sandbox Code Playgroud)

然后编辑新生成的迁移文件,

class CreateTablePeriodsUsers < ActiveRecord::Migration                                                                                                                                                                                                                                                                                                           
  def up
    create_table 'periods_users' do |t|
      t.integer :period_id
      t.integer :user_id
    end
  end

  def down
    drop_table 'periods_users'
  end
end
Run Code Online (Sandbox Code Playgroud)

运行rake db:migrate,你应该设置.

方法B:

$ rails generate model PeriodsToUser period_id:integer user_id:integer
Run Code Online (Sandbox Code Playgroud)

编辑PeriodsToUsers模型

class PeriodToUser < ActiveRecord::Base
  belongs_to :period
  belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)

然后更改模型以使用该:through选项.

class Period < ActiveRecord::Base
  # ...
  has_many :users, :through => :period_to_user
end

class User < ActiveRecord::Base
  # ...
  has_many :periods, :through => :period_to_user
end
Run Code Online (Sandbox Code Playgroud)

Ryan Bates在他的网站上有一个很好的例子,https://github.com/ryanb/railscasts/blob/master/app/models/tagging.rb