我有一个类,它定义了一张已打开的票和另一张已关闭的票:

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

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

我想把票移到closed_tickets表附近但是当模型执行验证时,它确实使用了超类表名,因此它们失败了。
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/

10-13 04:45