开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜