将其他属性添加到ActiveRecord序列化?

5 ruby serialization activerecord json ruby-on-rails

我的json序列化工作正常

render :json => "#{current_object.serialize(:json, :attributes => [:id, :name])}
Run Code Online (Sandbox Code Playgroud)

但是我还希望在它回到客户端之前将更多数据添加到json.主要是auth_token.

谷歌搜索疯狂,但我找不到序列化将采取什么选项允许我将我的其他数据追加/合并到JSON.

跳来找这样的东西......

current_object.serialize(:json, :attriubtes => [:id, name], :magic_option => {:form_authenticity_token => "#{form_authenticity_token}"})
Run Code Online (Sandbox Code Playgroud)

小智 0

将其破解并继续...

current_object.serialize(:json, :attributes => [:id, :name]).gsub(/\}$/, ", \"form_authenticity_token\": \"#{form_authenticity_token}\"}")
Run Code Online (Sandbox Code Playgroud)