识别 Rails 中两个模型之间多个一对一关系的关系

发布于 2024-10-12 10:44:55 字数 485 浏览 5 评论 0原文

我有以下两个模型:

class Project < ActiveRecord::Base
 has_one :start_date, :class_name => 'KeyDate', :dependent => :destroy
 has_one :end_date, :class_name => 'KeyDate', :dependent => :destroy

并且

class KeyDate < ActiveRecord::Base
 belongs_to :project

给定数据库中与项目相关的某个关键日期:

@key_date = KeyDate.find(:first)

是否有一种方法可以内省关系以检查 @key_date 是否与项目相关(作为 start_date 或 end_date)?

I have the following two models:

class Project < ActiveRecord::Base
 has_one :start_date, :class_name => 'KeyDate', :dependent => :destroy
 has_one :end_date, :class_name => 'KeyDate', :dependent => :destroy

and

class KeyDate < ActiveRecord::Base
 belongs_to :project

Given a certain key date from the database related to a project:

@key_date = KeyDate.find(:first)

is there a way to introspect the relationship to check if the @key_date is related to the project as start_date or as end_date?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

无需解释 2024-10-19 10:44:55

一个很好的方法是对 KeyDate 类使用单表继承

class KeyDate < ActiveRecord::Base
  belongs_to :project
end

class StartDate < KeyDate
end

class EndDate < KeyDate
end

class Project < ActiveRecord::Base
  has_one :start_date, :dependent => :destroy
  has_one :end_date, :dependent => :destroy
end

class CreateKeyDatesMigration < ActiveRecord::Migration
  def up
    create_table :key_dates do |t|
      t.date :date
      t.string :type #this is the magic column that activates single table inheritance
      t.references :project
    end
  end
  …
end

这可以让你做到

@key_date = KeyDate.find(:first)
@key_date.type # => "StartDate"

A nice way would be to use single table inheritance for the KeyDate class

class KeyDate < ActiveRecord::Base
  belongs_to :project
end

class StartDate < KeyDate
end

class EndDate < KeyDate
end

class Project < ActiveRecord::Base
  has_one :start_date, :dependent => :destroy
  has_one :end_date, :dependent => :destroy
end

class CreateKeyDatesMigration < ActiveRecord::Migration
  def up
    create_table :key_dates do |t|
      t.date :date
      t.string :type #this is the magic column that activates single table inheritance
      t.references :project
    end
  end
  …
end

this lets you do

@key_date = KeyDate.find(:first)
@key_date.type # => "StartDate"
安穩 2024-10-19 10:44:55

只是大声思考......

class KeyDate < ActiveRecord::Base
  belongs_to :project

  def start_date?
    project.start_date == self
  end

  def end_date?
    project.start_date == self
  end

  date_type
    [:start_date, :end_date].find {|sym| send("#{sym}?") }
  end
end

说实话,我不明白你为什么需要这个。无论如何,你肯定总是能掌控一个项目吗?

Just thinking aloud...

class KeyDate < ActiveRecord::Base
  belongs_to :project

  def start_date?
    project.start_date == self
  end

  def end_date?
    project.start_date == self
  end

  date_type
    [:start_date, :end_date].find {|sym| send("#{sym}?") }
  end
end

To be honest I can't see why you'd ever need this. Surely you're always going to have a handle on a project anyway?

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