jeudi 8 septembre 2016

Inherit from a model and change table for all purposes in Rails 3

I have a Class that defines an opened ticket and another one for closed tickets:

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

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

And I want to move the tickets that are closed to the closed_tickets table. But when the model performs the validations, it does using the superclass table name, and consequently they fail.

ClosedTicket.table_name => "closed_tickets"
ClosedTicket.create(Ticket.first.attributes)
SELECT FROM tickets WHERE id =...
=> ["ID has already been taken"]

I don't have a single clue on how to fix the table name for all methods and validations.

Aucun commentaire:

Enregistrer un commentaire