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
精彩评论