努力通过关系添加has_many

Struggling to add has_many through relationship

我正在尝试通过这样的关系建立 has_many:

#user.rb
class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :availabilities
  has_many :timeslots, :through => availabilities
end

#availability.rb
class Availability < ApplicationRecord
    belongs_to :timeslot
    belongs_to :user
end

#timeslot.rb
class Timeslot < ApplicationRecord
    has_many :availabilities
    has_many :timeslots, :through => availabilities
end

我创建了两个模型,然后 运行 rake db:migrate 没有在模型中添加代码(创建表格)。 我做了一个迁移文件:

class AddFieldsToTables < ActiveRecord::Migration[5.0]
  def change
    add_column :users, :availability_id, :integer
    add_column :timeslots, :availability_id, :integer
    add_column :availabilities, :user_id, :integer
    add_column :availabilities, :timeslot_id, :integer
  end
end

和运行 rake db:migrate 比我将上面的代码添加到所有文件中。 然后,如果我尝试生成任何东西,它会给我 NameError: undefined local variable or method availabilities for User (call 'User.connection' to establish a connection):Class

我是 Rails 的 Ruby 新手。

我在你的代码中发现了一个小问题:

#timeslot.rb
class Timeslot < ApplicationRecord
    has_many :availabilities
    has_many :timeslots, :through => availabilities
end

应该是:

#timeslot.rb
class Timeslot < ApplicationRecord
    has_many :availabilities
    has_many :users, :through => availabilities
end

我不确定它是否可以解决您的问题,但您的代码(排除上述错误)对我来说听起来不错。

我看到的一个问题是在您的 timeslot.rb 中您有 has_many :timeslots, :through => availabilities。我猜你想要 has_many :users, :through => :availabilites.

另一个在user.rb,你有has_many :timeslots, :through => availabilities但你需要符号:availabilites。我相信这就是导致您发布错误的原因。它应该看起来像这样(我只更改了倒数第二行):

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :availabilities
  has_many :timeslots, :through => :availabilities
end

为了在两个 tableuserstimeslots 之间设置 has_many through 关系,您需要设置 join table availabilitiesuser_idtimeslot_id

像下面这样设置您的 rails 模型:

# models/user.rb
class User < ApplicationRecord
  has_many :availabilities
  has_many :timeslots, :through => :availabilities
end

# models/availability.rb
# projects table should have these columns - user_id:integer, timeslot_id:integer
class Availability < ApplicationRecord
  belongs_to :timeslot
  belongs_to :user
end    

# models/timeslot.rb
class Timeslot < ApplicationRecord
  has_many :availabilities
  has_many :users, :through => :availabilities
end

您需要一个迁移来创建 availabilities table,它通过 Timeslot 对象和 [=] 之间的关系充当 has_many 的连接 table 20=] 对象。迁移文件看起来像这样:

class CreateAvailabilities < ActiveRecord::Migration[5.0]
  def change
    create_table :availabilities do |t|
      t.integer :user_id
      t.integer :timeslot_id
    end
  end
end

访问

User.last.timeslots 给出 0、1 或多个与 User.last

关联的时隙

Timeslot.last.users 给出 0、1 或许多与 Timeslot.last

关联的用户