Ruby on rails 视图是否不基于范围?
我在我的Ruby on rails 视图是否不基于范围?,ruby-on-rails,ruby,ruby-on-rails-3,Ruby On Rails,Ruby,Ruby On Rails 3,我在我的产品模型中定义了一个范围,即: class Product < ActiveRecord::Base attr_accessible :send_to_data # this is a boolean column scope :my_products, where(:send_to_data => true) end 类产品true) 结束 然后在我的控制器中: class ProductsController < ApplicationControlle
产品模型中定义了一个范围,即:
class Product < ActiveRecord::Base
attr_accessible :send_to_data # this is a boolean column
scope :my_products, where(:send_to_data => true)
end
类产品true)
结束
然后在我的控制器中:
class ProductsController < ApplicationController
def index
@my_products = current_user.products
end
end
class ProductsController
最后,我的看法是:
<% for product in @my_products %>
<%= product.user_id %>
<%= product.name %>
<%= product.send_to_data %>
<% end %>
但它仍然呈现所有产品,包括标记为false的:send_to_data
如何获得仅适用于我的范围的范围?命名范围必须直接用于以下产品:
def index
@my_products = current_user.products.my_products
end
命名范围不会更改“产品”关系的默认行为。当你想使用它时,必须用它的名字来称呼它。我还想将它重命名为更具描述性的名称,如“发送数据”,而不是“我的产品”。事实上,当前的_user.products.my_产品听起来是多余的。”“当前用户.products.send_data”可以更好地了解您试图获取的记录。