开发者

How to exclude fields of has_many associations when rendering xml

Part 1.

I have a Series which has_many :articles. In my show action, if xml is requested, I'd like to include all the associated :articles, but I really only want three of the fields: :title, :date, and :id

How can I do this?

Part 2.

Instead of doing this from the controller, I wonder if it would be better just to override to_xml in my model. Is this good practice? How would I do this?

Thanks so much!

Edit

Sector was almost right, but it needs to be a hash:

render :xml => @series.to_xml(:include => { 开发者_开发知识库:articles => { :only => [:title, :date, :id] } })


Part 1

respond_to do |format|
  format.xml {
    render :xml => @series.to_xml(:include => { :articles => { :only => [:title, :date, :id] } })
  }
end

Part 2

Controller is good place for this

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜