开发者

STI change amongs inherited types.

开发者 https://www.devze.com 2023-03-24 02:56 出处:网络
In my models I use STI like this Vehicle Model: vehicle.rb class Veh开发者_开发技巧icle < ActiveRecord::Base

In my models I use STI like this

Vehicle Model: vehicle.rb

class Veh开发者_开发技巧icle < ActiveRecord::Base
end

Car Model: car.rb

class Car < Vehicle
end

Bus Model: bus.rb

class Bus < Vehicle
end

If I create a Car can I somehow change it's type to Vehicle or Bus?


To permanently alter the type, change the value of the type column.

c1 = Car.first
c1.name # BMW

c1.update_attribute(:type, "Bus")

b1 = Bus.first
b1.name # BMW

To also change the object type in-memory without reloading it from the DB, use "becomes, as in

c1 = c1.becomes(Bus)
0

精彩评论

暂无评论...
验证码 换一张
取 消