使用 has_many AND has_one 将两个表链接在一起?

发布于 2024-09-19 12:28:42 字数 1417 浏览 4 评论 0原文

我确信这个问题已经被问过一百万次了。我只是搜索得不太好。我有以下设置:我有一位有很多活动的教练。每个活动只有一名讲师(所有者/创建者)。我想添加一个单独的链接,以允许其他讲师关联,但仍保留原始讲师(所有者)。我有一个粗略的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹿! 2024-09-26 12:28:42

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 an is_primary_instructor bit field to the associated events table?

烟花肆意 2024-09-26 12:28:42

同意什洛莫的观点。

class Instructor < ActiveRecord::Base
  has_many :instruct_events
  has_many events, :through => :instruct_events
end

class InstructEvent < ActiveRecord::Base
  belongs_to :instructor
  belongs_to :event
end

class Event < ActiveRecord::Base
  has_many :instruct_events
  has_many :instructors, :through => :instruct_events
end

class CreateInstructors < ActiveRecord::Migration
  def self.up
    create_table :instructors do |t|
      # name, email, etc
      t.timestamps
    end
  end
end

class CreateInstructEvents < ActiveRecord::Migration
  def self.up
    create_table :instruct_events do |t|
      t.integer :instructor_id
      t.integer :event_id

      t.boolean :is_primary_instructor
      t.timestamps
    end
  end
end

class CreateEvents < ActiveRecord::Migration
  def self.up
    create_table :events do |t|
      # title, location, etc
      t.timestamps
    end
  end
end

所以每个教练都有很多项目,每个项目都有很多教练。但你必须确定一名教练作为主要教练。

=====更新=====

要引用主要讲师:

class InstructEvent
  # add this:
  named_scope :primary, :conditions => { :is_primary_instructor => true }
end

给定一个事件,找到该事件的主要讲师:

event.instruct_events.primary.instructor

给定一个讲师,找到该讲师是主要的事件:

instructor.instruct_events.primary.event

也许你可以给 InstructEvents 类一个更好的名字。

我也希望看到更多漂亮的解决方案:D

Agree with Shlomo.

class Instructor < ActiveRecord::Base
  has_many :instruct_events
  has_many events, :through => :instruct_events
end

class InstructEvent < ActiveRecord::Base
  belongs_to :instructor
  belongs_to :event
end

class Event < ActiveRecord::Base
  has_many :instruct_events
  has_many :instructors, :through => :instruct_events
end

class CreateInstructors < ActiveRecord::Migration
  def self.up
    create_table :instructors do |t|
      # name, email, etc
      t.timestamps
    end
  end
end

class CreateInstructEvents < ActiveRecord::Migration
  def self.up
    create_table :instruct_events do |t|
      t.integer :instructor_id
      t.integer :event_id

      t.boolean :is_primary_instructor
      t.timestamps
    end
  end
end

class CreateEvents < ActiveRecord::Migration
  def self.up
    create_table :events do |t|
      # title, location, etc
      t.timestamps
    end
  end
end

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:

class InstructEvent
  # add this:
  named_scope :primary, :conditions => { :is_primary_instructor => true }
end

given an event, find the primary instructor of the event:

event.instruct_events.primary.instructor

given an instructor, find the event which the instructor is primary:

instructor.instruct_events.primary.event

Maybe you could give the InstructEvents class a better name.

And also I hope to see more beautiful solutions too :D

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文