Ruby on Rails - 问题 ActiveRecord、迁移和同一表的两列

发布于 2024-10-20 09:18:42 字数 1842 浏览 3 评论 0原文

好吧,我拥有的是一个记录“人”历史记录的表,它记录了人(用户)、处理程序(用户)、之前的状态(JobApplicationStatus)、之后的状态(JobApplicationStatus)。

现在在我的脑海中,这可以转化为这样的表格:

**JobApplicationHistory**
id (int)
status_before_id (int)
status_after_id (int)
user_id (int)
handler_id (int)

我尝试进行迁移,这有点有效,但效果不佳。

因为我想使用类似的东西:

user = User.find(1)
handler = User.find(1)
status_before = JobApplicationStatus.find(1)
status_after = JobApplicationStatus.find(2)

history = JobApplicationHistory.new()
history.user = user
history.handler = handler
history.status_before = status_before
history.status_after = status_after
history.save

这是我的迁移

class CreateUserApplicationHistories < ActiveRecord::Migration
  def self.up
    create_table :user_application_histories do |t|
      t.integer :user_id # goes to User
      t.references :job # goes to Job
      t.integer :handler_id # goes to User
      t.integer :status_from_id # goes to JobApplicationStatus
      t.integer :status_to_id # goes to JobApplicationStatus
      t.timestamps
    end

add_index("user_application_histories", "job_id")
add_index("user_application_histories", "handler_id")
add_index("user_application_histories", "user_id")
add_index("user_application_histories", "status_from_id")
add_index("user_application_histories", "status_to_id")

结束

def self.down drop_table:用户应用程序历史记录 结尾 结尾

我的模型,我认为它会失败

class UserApplicationHistory < ActiveRecord::Base
  belongs_to :status_from_id, :class_name => "JobApplicationStatus"
    belongs_to :status_to_id, :class_name => "JobApplicationStatus"
  belongs_to :user_id, :class_name => "User"
  belongs_to :handler_id, :class_name => "User"
end

Okay so what I have is a table that keeps track of history on a "person", this logs the person(User), the handler(User), the status before(JobApplicationStatus), the status after(JobApplicationStatus).

Now in my head this translates down to a table of such:

**JobApplicationHistory**
id (int)
status_before_id (int)
status_after_id (int)
user_id (int)
handler_id (int)

I tried to make a migation, that sorta worked, but it's not working right.

As I would like to use something like:

user = User.find(1)
handler = User.find(1)
status_before = JobApplicationStatus.find(1)
status_after = JobApplicationStatus.find(2)

history = JobApplicationHistory.new()
history.user = user
history.handler = handler
history.status_before = status_before
history.status_after = status_after
history.save

Here is my migration

class CreateUserApplicationHistories < ActiveRecord::Migration
  def self.up
    create_table :user_application_histories do |t|
      t.integer :user_id # goes to User
      t.references :job # goes to Job
      t.integer :handler_id # goes to User
      t.integer :status_from_id # goes to JobApplicationStatus
      t.integer :status_to_id # goes to JobApplicationStatus
      t.timestamps
    end

add_index("user_application_histories", "job_id")
add_index("user_application_histories", "handler_id")
add_index("user_application_histories", "user_id")
add_index("user_application_histories", "status_from_id")
add_index("user_application_histories", "status_to_id")

end

def self.down
drop_table :user_application_histories
end
end

And my model, which i think makes it fail

class UserApplicationHistory < ActiveRecord::Base
  belongs_to :status_from_id, :class_name => "JobApplicationStatus"
    belongs_to :status_to_id, :class_name => "JobApplicationStatus"
  belongs_to :user_id, :class_name => "User"
  belongs_to :handler_id, :class_name => "User"
end

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

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

发布评论

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

评论(1

寂寞陪衬 2024-10-27 09:18:42

你是对的,对于用户和处理程序来说,你的模型应该是这样的:

belongs_to :user
belongs_to :handler, :class_name => "User"

为了帮助你使用 JobApplicationStatus,我需要知道这个表的样子

You right, your model should look like this for user and handler:

belongs_to :user
belongs_to :handler, :class_name => "User"

To help you with JobApplicationStatus I would need to know how this table looks like

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