根据“子节点”下的 RABL 文档,以下是可能的:
object @user
child :posts do |user|
attribute :title unless user.suspended?
end
这意味着user
块产生的变量是父对象@user
。
但是,当我尝试以下操作时:
collection @listings
child :address do |listing|
attribute :number_and_street unless listing.address_hidden?
end
我得到一个NoMethodError
:
undefined method `address_hidden?' for #<Address:0x007fb83d6eaf80>
这意味着该块正在产生子地址对象而不是父@listing
对象,正如文档所暗示的那样。
我能看到的唯一解决方法是类似address.listing.address_hidden?
,这会导致数据库查询过多,所以我想避免这种情况。
难道我做错了什么?有没有办法解决这种行为?