0

我正在尝试覆盖 json 的默认模型值,但不是覆盖它,而是创建重复的哈希

我的模型:

class HomeScreenButton < ActiveRecord::Base
    belongs_to :product_category    
    validates :product_category_id, :x, :y, :presence => true
  attr_accessible :product_category_id, :x, :y

  def as_json(options={})
    hash = super(options)
    hash.merge({
      :product_category_id => "fdfd"
    })
  end
end

我的控制器:

def index
    @home_screen_buttons = HomeScreenButton.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @home_screen_buttons}
    end
end

当我打开 json 时,它会显示 product_category_id 的重复项: [{"created_at":"2013-03-17T11:14:32Z","id":1,"product_category_id":5,"updated_at":"2013-03-17T11:14:32Z","x":300,"y":200,"product_category_id":"dfdffff"}]

4

1 回答 1

1

无需合并哈希

def as_json(options={})
  hash = super(options)
  hash[:product_category_id] = "fdfd"
  hash
end
于 2013-03-20T20:45:09.977 回答