开发者

Rails 3: problem with as_json in combination with include

I have two models in a 1:n relation. Both are put out in JSON only. Therefore I defined as_json in both models:

class Foo < ActiveRecord::Base
  has_many :foos, dependent: :destroy
  def as_json options={}
    super except: [:created_at, :updated_at, :id, :user_id], include: options[:include]
  end
end


class Bar < ActiveRecord::Base
  belongs_to :foo
  def as_json options={}
    super except: [:id, :foo_id, :created_at, :updated_at], include: options[:include]
  end
end

Now I request foo's and bar's independently and as_json works as excepted. But in case I request bar's with foo's included the response contains foo's with attributes that should not be there. In fact as_json is开发者_如何转开发 not even called.

This seems to be standard behaviour. But how can I turn it off / achive my goal to always use as_json independently, wether include is used or not?

I appreciate every kind of hint, link or answer that helps solving this question.

Thx in advance. Felix

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜