Hopstream
Hopstream

Reputation: 6451

Rails single table inheritance change type of object?

I have the following STI models:

class Question < ActiveRecord::Base
class Unapproved < Question
class Approved < Question

If I have a question that is of type Unapproved, how can I convert it into of type Approved?

Upvotes: 0

Views: 946

Answers (2)

davetapley
davetapley

Reputation: 17898

See the becomes and becomes! methods, e.g:

Unapproved.first.becomes!(Approved)

Upvotes: 4

oreoluwa
oreoluwa

Reputation: 5623

I don't think Rails provides a method for autoswitching, but you should be able to do:

Approved.first.update(type: 'Unapproved')

OR

question = Approved.find(x)
question.type = 'Unapproved'
question.save

You could define the autoswitcher yourself in your Question class too

Upvotes: 1

Related Questions