使用 has_many AND has_one 将两个表链接在一起?
我确信这个问题已经被问过一百万次了。我只是搜索得不太好。我有以下设置:我有一位有很多活动的教练。每个活动只有一名讲师(所有者/创建者)。我想添加一个单独的链接,以允许其他讲师关联,但仍保留原始讲师(所有者)。我有一个粗略的 ASCII 表示,但我一生都无法弄清楚如何在 Rails 模型中做到这一点。
,-------------------.
| other_instructors |
|-------------------|
| event_id |-------------.
,---------------| instructor_id | |
| `-------------------' |
| ,---------------------. |
`->>| instructor |<--. |
|---------------------| | ,------------. |
| name | | | event |<-'
| email | | |------------|
| office | | | title |
| phone | | | location |
| admin? | | | benefit |
| notify_recipient? | | | notes |
| new_user? | | | start_time |
| (acts_as_authentic) | | | end_time |
`---------------------' | | live_in? |
`---| instructor |
`------------'
@instructor.events = [... array of events ...]
@associated_events = [... array of events associated but not owned via other_instructors table ...]
@event.instructor = ... One Instructor ...
@event.other_instructors = [... array of other instructors ...]
I sure this has been asked a million times already. I just not searching very well. I have the following setup: I have an Instructor who has many Events. Each event has only one Instructor (owner/creator). I want to add a separate linkage to allow other instructors to be associated but still preserve the original instructor (owner). I have a crude ASCII representation but I can't for the life of me figure out how to do that in a rails model.
,-------------------.
| other_instructors |
|-------------------|
| event_id |-------------.
,---------------| instructor_id | |
| `-------------------' |
| ,---------------------. |
`->>| instructor |<--. |
|---------------------| | ,------------. |
| name | | | event |<-'
| email | | |------------|
| office | | | title |
| phone | | | location |
| admin? | | | benefit |
| notify_recipient? | | | notes |
| new_user? | | | start_time |
| (acts_as_authentic) | | | end_time |
`---------------------' | | live_in? |
`---| instructor |
`------------'
@instructor.events = [... array of events ...]
@associated_events = [... array of events associated but not owned via other_instructors table ...]
@event.instructor = ... One Instructor ...
@event.other_instructors = [... array of other instructors ...]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ASCII 为 2 分。
我对 Rails 一无所知,但是从
event
表中取出讲师外部引用,并向关联的关联添加一个is_primary_instructor
位字段是否更有意义?事件表?2 points for the ASCII.
I don't know anything about Rails, but would it make more sense to take the instructor foreign reference off of the
event
table, and add anis_primary_instructor
bit field to the associated events table?同意什洛莫的观点。
所以每个教练都有很多项目,每个项目都有很多教练。但你必须确定一名教练作为主要教练。
=====更新=====
要引用主要讲师:
给定一个事件,找到该事件的主要讲师:
给定一个讲师,找到该讲师是主要的事件:
也许你可以给 InstructEvents 类一个更好的名字。
我也希望看到更多漂亮的解决方案:D
Agree with Shlomo.
So every instructor has many events, and every event has many instructor. But you have to identify one instruct as the primary instructor.
===== UPDATE =====
To reference the primary instructor:
given an event, find the primary instructor of the event:
given an instructor, find the event which the instructor is primary:
Maybe you could give the InstructEvents class a better name.
And also I hope to see more beautiful solutions too :D