[英]Struggling to add has_many through relationship
I am trying to make a has_many through relationship like this: 我试图通过这样的关系来建立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
I created the two models and than ran rake db:migrate
without adding the code in the models (to create the tables). 我创建了两个模型,然后运行了
rake db:migrate
而没有在模型中添加代码(创建表)。 I made a migration file: 我做了一个迁移文件:
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
and ran rake db:migrate
Than I added the code above to all the files. 并运行
rake db:migrate
将上述代码添加到所有文件中。 And then if I try to generate anything it gives me NameError: undefined local variable or method availabilities for User (call 'User.connection' to establish a connection):Class
然后,如果我尝试生成任何东西,它将给我
NameError: undefined local variable or method availabilities for User (call 'User.connection' to establish a connection):Class
I am new to Ruby on Rails. 我是Ruby on Rails的新手。
I see a tiny problem in your code: 我在您的代码中看到一个小问题:
#timeslot.rb
class Timeslot < ApplicationRecord
has_many :availabilities
has_many :timeslots, :through => availabilities
end
it should be: 它应该是:
#timeslot.rb
class Timeslot < ApplicationRecord
has_many :availabilities
has_many :users, :through => availabilities
end
I'm not sure if it can solve your problem but your code (exclude the above mistake) sounds fine for me. 我不确定它是否可以解决您的问题,但是您的代码(排除上述错误)对我来说听起来不错。
One issue I see is that in your timeslot.rb
you have has_many :timeslots, :through => availabilities
. 我看到的一个问题是,在您的
timeslot.rb
您具有has_many :timeslots, :through => availabilities
。 I'm guessing you want has_many :users, :through => :availabilites
. 我猜你想要
has_many :users, :through => :availabilites
。
Another is in user.rb
, you have has_many :timeslots, :through => availabilities
but you need the symbol :availabilites
. 另一个是在
user.rb
,您具有has_many :timeslots, :through => availabilities
但是需要符号:availabilites
。 This is what is causing the error you posted, I believe. 我相信,这就是导致您发布的错误的原因。 It should look like this (all I've changed is the second-to-last line):
它看起来应该像这样(我更改的是倒数第二行):
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
In order to setup has_many through
relationship between two tables users
and timeslots
, you need to setup join table availabilities
with columns user_id
and timeslot_id
. 为了
has_many through
两个表users
和timeslots
之间的关系来设置has_many through
,您需要使用user_id
和timeslot_id
列来设置联接表的availabilities
。
Setup your rails models like below: 如下设置您的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
You need a migration to create availabilities
table which acts as a join table for your has_many through relationship between Timeslot
Object and User
Object. 您需要迁移才能创建
availabilities
表,该表通过Timeslot
对象和User
对象之间的关系充当has_many的联接表。 Migration file looks something like this: 迁移文件如下所示:
class CreateAvailabilities < ActiveRecord::Migration[5.0]
def change
create_table :availabilities do |t|
t.integer :user_id
t.integer :timeslot_id
end
end
end
Access 访问
User.last.timeslots
gives 0, 1 or many timeslots associated with User.last
User.last.timeslots
给出与User.last
关联的User.last
或许多时隙
Timeslot.last.users
gives 0, 1 or many users associated with Timeslot.last
Timeslot.last.users
提供0、1个或多个与Timeslot.last
相关的用户
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.