ruby-on-rails - 如何处理具有 has_many 和 belongs_to 的关系
问题描述
我User
有谁能做一个Event
。这些是组织者。
其他人可以作为参加者User
去这些人。Event
我应该如何做以下关系?
用户:
has_many :events, foreign_key: organizer_id
belongs_to :event
事件:
belongs_to :user #how to do the organizer and attendees distinction?
解决方案
我建议创建一个表并在表event_attends
中放置user_id
和event_id
列。然后你可以像这样创建关联:user_id
event_attend_id
events
class User < ActiveRecord::Base
has_many :event_attends
has_many :events
end
class Event < ActiveRecord::Base
belongs_to :user
has_many :event_attends
end
class EventAttend < ActiveRecord::Base
belongs_to :user
belongs_to :event
end
推荐阅读
- sql-server - SQL Server - 来自 DATETIME 的 DATE 转换是不确定的,但仅在用户定义的函数中
- json - Magento 邮递员无法保存产品
- spring-boot - Spring Security Authentication Server 具有多个用于 client_credentials 的身份验证提供程序
- shopify - Shopify App Bridge 和 embed in shopify admin 有什么区别
- javascript - 条件问题 NetSuite 电子邮件从 SuiteScript 与使用 Freemarker 的模板合并
- node.js - express-jwt 中间件在与 express 路由器一起使用时使用完整路径而不是子路径
- visual-studio-2017 - 修复 MASM 8086 链接器:错误 LNK2019:未解析的外部符号 _putchar
- javascript - 删除单选按钮中的默认选中值并将其替换为单击的另一个单选按钮
- json - JSON 模式中的 `additionalProperties` 规则不适用于嵌套级别属性
- javascript - 导出路由器模块?