gpt4 book ai didi

ruby-on-rails - 在 Rails 3 中为所有目的继承模型和更改表

转载 作者:太空宇宙 更新时间:2023-11-03 16:43:29 25 4
gpt4 key购买 nike

我有一个类定义了打开的工单和另一个用于关闭的工单:

class Ticket < ActiveRecord::Base
[validations]
[callbacks]
end

class ClosedTicket < Ticket
self.table_name = "closed_tickets"
end

我想将关闭的工单移动到closed_tickets 表中。但是当模型执行验证时,它确实使用父类(super class)表名,因此它们失败了。

ClosedTicket.table_name => "closed_tickets"
ClosedTicket.create(Ticket.first.attributes)
Ticket Exists (0.8ms) SELECT 1 AS one FROM `tickets` WHERE...
=> ["ID has already been taken"]

ClosedTicket.table_name => "closed_tickets"
closed_ticket = ClosedTicket.new(Ticket.first.attributes)
closed_ticket.save
Ticket Exists (0.8ms) SELECT 1 AS one FROM `tickets` WHERE...

对于如何修复所有方法和验证的表名,我没有任何线索。

最佳答案

ClosedTicket.table_name => "closed_tickets"
closed_ticket = ClosedTicket.new(Ticket.first.attributes)
closed_ticket.save

关于ruby-on-rails - 在 Rails 3 中为所有目的继承模型和更改表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39400296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com