限制 Rails 控制器响应中的特定字段

发布于 2024-11-27 17:35:30 字数 335 浏览 3 评论 0原文

我有一个像 Video 这样的控制器操作

def index
  @videos =  Video.all.to_a

  respond_to do |format|
    format.xml  { render :xml => @videos }
    format.json { render :json => @videos }
  end
end

,具有属性 nametitle

我希望返回 xml 仅包含 title

我如何限制它的响应。

I have a controller action like

def index
  @videos =  Video.all.to_a

  respond_to do |format|
    format.xml  { render :xml => @videos }
    format.json { render :json => @videos }
  end
end

Video has attributes name and title.

I want the return xml to contain only title.

How do I restrict it from the response.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无妨# 2024-12-04 17:35:30

这样做:

def index
  @videos =  Video.all

  respond_to do |format|
    format.xml  { render :xml => @videos.to_xml( :only => [:title] ) }
    format.json { render :json => @videos.to_json( :only => [:title] ) }
  end
end

您可以在序列化文档中找到更多相关信息。

Doing it like this:

def index
  @videos =  Video.all

  respond_to do |format|
    format.xml  { render :xml => @videos.to_xml( :only => [:title] ) }
    format.json { render :json => @videos.to_json( :only => [:title] ) }
  end
end

You can find more info about this at the serialization documentation.

情魔剑神 2024-12-04 17:35:30

您可以在 Video.all 查询中使用 select 子句,指定要包含的字段。

@videos = Video.select("id, name, title").all

此外,您不需要在查询中调用 to_a

You can use a select clause on your Video.all query, specifying the fields you want to include.

@videos = Video.select("id, name, title").all

Also, you shouldn't need to call to_a on your query.

硪扪都還晓 2024-12-04 17:35:30

您可以在video.rb中定义自己的.to_xml方法,

例如:

class Video < ActiveRecord::Base

  def to_xml(opts={})
    opts.merge!(:only => [:id, :title])
    super(opts)
  end

end

然后在控制器中调用respond_with(@videos)

请参阅此类似问题

You can define your own .to_xml method inside video.rb,

e.g:

class Video < ActiveRecord::Base

  def to_xml(opts={})
    opts.merge!(:only => [:id, :title])
    super(opts)
  end

end

And then call respond_with(@videos) in you controller.

See this similar question.

迷爱 2024-12-04 17:35:30

一种快速的方法是使用 :pluck ,如果你只是返回一个标题数组(我猜没有 :id),那么这会非常快

def index
  @titles = Video.pluck(:title)

  respond_to do |format|
    format.xml  { render :xml => @titles }
    format.json { render :json => @titles }
  end
end

:pluck将比任何其他选项快得多,因为它返回一个仅包含所请求数据的数组。它不会为每个数据库行实例化整个 ActiveRecord 对象。因为它是红宝石,所以这些实例化花费了大部分时间。你也可以这样做:

@videos_ary = Video.pluck(:id, :title)
response = @videos_ary.map {|va| { id: va[0], title: va[1] }}

如果你不想拿出 SQL 铅笔,这很好

a fast way would be to use :pluck, if you are just returning an array of titles (I am guessing no :id) , then this would be very fast

def index
  @titles = Video.pluck(:title)

  respond_to do |format|
    format.xml  { render :xml => @titles }
    format.json { render :json => @titles }
  end
end

:pluck will be way faster than any of the other options because it returns an array with just the data requested. It doesn't instantiate an entire ActiveRecord Object for each database row. Because its ruby, those instantiations are what take most of the time. You can also do :

@videos_ary = Video.pluck(:id, :title)
response = @videos_ary.map {|va| { id: va[0], title: va[1] }}

if you dont want to get your SQL pencil out, this is pretty good

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文