[英]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
我是Ruby on Rails的新手。
我在您的代码中看到一个小问题:
#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
为了has_many through
两个表users
和timeslots
之间的关系来设置has_many through
,您需要使用user_id
和timeslot_id
列来设置联接表的availabilities
。
如下设置您的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
表,该表通过Timeslot
对象和User
对象之间的关系充当has_many的联接表。 迁移文件如下所示:
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
给出与User.last
关联的User.last
或许多时隙
Timeslot.last.users
提供0、1个或多个与Timeslot.last
相关的用户
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.