gpt4 book ai didi

mysql - 如何向 Rails 中的关联表列添加新属性

转载 作者:行者123 更新时间:2023-11-29 14:09:41 26 4
gpt4 key购买 nike

在我的 Rails 应用程序中,我必须为关联表添加一个名为 is_leader 的新列。

关联表的关系如下:

has_and_belongs_to_many :analysis_responses, :join_table => "analysis_responses_participants"

以下是将参与者详细信息保存到数据库的代码:

organization.people.create(participant)

参与者已获得以下值

name: Test User
position:
birthdate:
id:
lead: "1"

如果引导值为 1,则特定记录的 is_leader 列值应为 1

我想知道如何保存 Rails 关联表中的 is_leader

谢谢

最佳答案

如果您需要在联接表上保存属性,则必须使用联接模型而不是 HABTM。

class Organization
has_many :analysis_responses
has_many :people, through: :analysis_responses
end

class AnalysisResponse
belongs_to :organization
belongs_to :person
end

class Person
has_many :analysis_responses
has_many :organizations, through: :analysis_reponses
end

关于mysql - 如何向 Rails 中的关联表列添加新属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13717542/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com