开发者

What's the best way to strip created_at and updated_at from an ActiveRecord instance?

开发者 https://www.devze.com 2022-12-22 10:40 出处:网络
I need to strip the created_at and updated_at fields from an instance before it\'s serialised and pushed to a client. What is the neatest way to ac开发者_运维百科hieve this?

I need to strip the created_at and updated_at fields from an instance before it's serialised and pushed to a client. What is the neatest way to ac开发者_运维百科hieve this?

Thanks,

Chris


A couple of ideas. Assuming for the examples that the model has name, email and timestamps.

When fetching the record do:

Model.find :select => [:name, :email]

i.e. not including the timestamps fields.

If you are serializing through JSON do:

model.to_json :only => [:name, :email]

or

model.to_json :except => [:created_at, :updated_at]
0

精彩评论

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