Ruby on Rails - 问题 ActiveRecord、迁移和同一表的两列
好吧,我拥有的是一个记录“人”历史记录的表,它记录了人(用户)、处理程序(用户)、之前的状态(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 endadd_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你是对的,对于用户和处理程序来说,你的模型应该是这样的:
为了帮助你使用 JobApplicationStatus,我需要知道这个表的样子
You right, your model should look like this for user and handler:
To help you with JobApplicationStatus I would need to know how this table looks like