LearningRoR
LearningRoR

Reputation: 27192

View is not based on scope?

I defined a scope in my Product model that is:

class Product < ActiveRecord::Base
  attr_accessible :send_to_data # this is a boolean column
  scope :my_products, where(:send_to_data => true)
end

Then in my controller:

class ProductsController < ApplicationController
  def index
    @my_products = current_user.products
  end
end

Finally my View:

<% for product in @my_products %>
   <%= product.user_id %>
   <%= product.name %>
   <%= product.send_to_data %>
<% end %>

But it still renders ALL Products including the ones flagged as false for :send_to_data.

How do I get the ones for my scope only?

Upvotes: 0

Views: 106

Answers (1)

robotcookies
robotcookies

Reputation: 1059

The named scope has to be used directly on products like so:

def index
  @my_products = current_user.products.my_products
end

A named scope doesn't change the default behavior of the 'products' relation. It has to be called by its name when you want to use it.

Upvotes: 2

Related Questions