努力通过关系添加has_many

时间:2017-03-02 01:58:42

标签: mysql ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-5

我正试图通过这样的关系建立一个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

我是Ruby on Rails的新手。

3 个答案:

答案 0 :(得分:1)

我发现代码中存在一个小问题:

#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

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

答案 1 :(得分:1)

我看到的一个问题是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

答案 2 :(得分:0)

要在两个表app loadhas_many through之间设置users关系,您需要使用列timeslots和{{1}设置联接表availabilities }}。

设置您的导轨模型,如下所示:

user_id

您需要迁移才能创建timeslot_id表,该表充当您在# 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对象之间的has_many关系的连接表。迁移文件如下所示:

Timeslot

使用

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

相关联的0,1或多个时隙

User.last.timeslots提供与User.last

相关联的0,1或多个用户