使用一个模型和另一个模型的两个属性在 Rails 中创建联接 Table
Creating Join Table in Rails with One Model and Two Attributes on Another Model
正在尝试创建一个连接 table,该连接在一个模型上采用两个属性,并将它们与第二个模型的 ID 相关联。由于持续刷新过程,无法使用第一个模型的 ID,该过程重新排序该模型 table (ConstantlyRefreshingModels) 上的行。
class ConstantlyRefreshingModels < ApplicationRecord
attr_accessor :home_id, :chair, :floor, :number
has_and_belongs_to_many :family
end
class Family < ApplicationRecord
attr_accessor :bread_winner
has_and_belongs_to_many :constantlyrefreshingmodel
end
class CreateFamilyModelThatConstantlyRefreshesJoinTable < ActiveRecord::Migration[5.0]
def change
create_join_table :families, :constantlyrefreshingmodel
end
end
正在寻找如下所示的联接 table:
home_id | number | family_id
-----------------------------
2 | '3003' | 4
1 | '2100' | 1
然后这对公开数据有效:
new_home = constantlyrefreshingmodel.where(home_id: 2)
new_home.families == 4 == true
关于创建连接 table 和声明关联有什么建议吗?
您需要 has_many :through Association
而不是 has_and_belongs_to_many
,因为您在连接模型中有其他属性 (:number
)
You should use has_many :through
if you need validations, callbacks or extra attributes on the join model. Section 2.8
试试这个方法:
class Home < ApplicationRecord
has_many :points
has_many :families, through: :points
end
# Point is your join model, AKA ConstantlyRefreshingModel
class Point < ApplicationRecord
belongs_to :family
belongs_to :home
validates :points, presence: true # Or some other validation, :number in your question.
end
class Family < ApplicationRecord
has_many :points
has_many :houses, through: :points
end
加入table迁移:
class CreatePoints < ActiveRecord::Migration[5.2]
def change
create_table :points do |t|
t.references :home, foreign_key: true, null: false
t.references :family, foreign_key: true, null: false
t.string :points, null: false
t.timestamps
end
end
end
例如有以下数据:
home_1 = Home.create(id: 1) # home with an id of 1
home_2 = Home.create(id: 2) # home with an id of 2
family_4 = Family.create(id: 4) # family with an id of 4
family_1 = Family.create(id: 1) # family with an id of 1
Point.create(points: "3003", home: home_2, family: family_4)
Point.create(points: "2100", home: home_1, family: family_1)
然后:
points = Point.where(home: home_2) # you get a collection of all points assigned to house with ID: 2
points.first.family # family with an id of 4
揭露 :home
与 id: 2
和数字 3003
指向 id: 4
的家庭
home = Home.find(2)
home.families.ids # 4
home.points.first # 3003
要公开具有 id: 4
的家庭与 ID 为 2 且编号为 3003 的家庭相关联。
family = Family.find(4)
family.homes.ids # 2
family.points.first # 3003
此外,attr_accessor
、attr_reader
和 attr_writer
在 Rails 5.1
中已弃用。参见 this post
正在尝试创建一个连接 table,该连接在一个模型上采用两个属性,并将它们与第二个模型的 ID 相关联。由于持续刷新过程,无法使用第一个模型的 ID,该过程重新排序该模型 table (ConstantlyRefreshingModels) 上的行。
class ConstantlyRefreshingModels < ApplicationRecord
attr_accessor :home_id, :chair, :floor, :number
has_and_belongs_to_many :family
end
class Family < ApplicationRecord
attr_accessor :bread_winner
has_and_belongs_to_many :constantlyrefreshingmodel
end
class CreateFamilyModelThatConstantlyRefreshesJoinTable < ActiveRecord::Migration[5.0]
def change
create_join_table :families, :constantlyrefreshingmodel
end
end
正在寻找如下所示的联接 table:
home_id | number | family_id
-----------------------------
2 | '3003' | 4
1 | '2100' | 1
然后这对公开数据有效:
new_home = constantlyrefreshingmodel.where(home_id: 2)
new_home.families == 4 == true
关于创建连接 table 和声明关联有什么建议吗?
您需要 has_many :through Association
而不是 has_and_belongs_to_many
,因为您在连接模型中有其他属性 (:number
)
You should
use has_many :through
if you need validations, callbacks or extra attributes on the join model. Section 2.8
试试这个方法:
class Home < ApplicationRecord
has_many :points
has_many :families, through: :points
end
# Point is your join model, AKA ConstantlyRefreshingModel
class Point < ApplicationRecord
belongs_to :family
belongs_to :home
validates :points, presence: true # Or some other validation, :number in your question.
end
class Family < ApplicationRecord
has_many :points
has_many :houses, through: :points
end
加入table迁移:
class CreatePoints < ActiveRecord::Migration[5.2]
def change
create_table :points do |t|
t.references :home, foreign_key: true, null: false
t.references :family, foreign_key: true, null: false
t.string :points, null: false
t.timestamps
end
end
end
例如有以下数据:
home_1 = Home.create(id: 1) # home with an id of 1
home_2 = Home.create(id: 2) # home with an id of 2
family_4 = Family.create(id: 4) # family with an id of 4
family_1 = Family.create(id: 1) # family with an id of 1
Point.create(points: "3003", home: home_2, family: family_4)
Point.create(points: "2100", home: home_1, family: family_1)
然后:
points = Point.where(home: home_2) # you get a collection of all points assigned to house with ID: 2
points.first.family # family with an id of 4
揭露 :home
与 id: 2
和数字 3003
指向 id: 4
home = Home.find(2)
home.families.ids # 4
home.points.first # 3003
要公开具有 id: 4
的家庭与 ID 为 2 且编号为 3003 的家庭相关联。
family = Family.find(4)
family.homes.ids # 2
family.points.first # 3003
此外,attr_accessor
、attr_reader
和 attr_writer
在 Rails 5.1
中已弃用。参见 this post