gpt4 book ai didi

ruby-on-rails - 我如何在 Rails 3 的 Controller 中使用 include 来过滤 View 中的结果?

转载 作者:行者123 更新时间:2023-12-04 03:12:39 25 4
gpt4 key购买 nike

我的 Controller 里有这个:

class User::ResourcesController < User::UserController
def index
@resource_type = ResourceType.find_by_name(params[:resource_type].to_s.titleize)
@products = @products.includes(:resources).where(:resources => { :resource_type_id => @resource_type.id })

respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @resources }
end
end
end

我正在尝试过滤我的资源,因此在我看来我可以使用下面的代码并让它只提取具有正确 resource_type_id 的资源。

@products.each do |product|
product.resources.count
end

最佳答案

@products = Product.includes(:resources).where("resources.resource_type_id = ?", @resource_type.id)

关于ruby-on-rails - 我如何在 Rails 3 的 Controller 中使用 include 来过滤 View 中的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5495008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com