In my model (pins.rb), I have two sorting orders:
default_scope order: 'pins.featured DESC' #for adding featured posts to the top of a list
default_scope order: 'pins.created_at DESC' #for adding the remaining posts beneath the featured posts
This sorting order (above) is how I want my 'pins view' (index.html.erb) to look. Which is just a list of ALL user posts.
In my 'users view' (show.html.erb) I am using the same model (pins.rb) to list only current_user pins. HOWEVER, I want to sorting order to ignore the "featured" default scope and only use the second scope:
default_scope order: 'pins.created_at DESC'
How can I accomplish this? I tried doing something like this:
default_scope order: 'pins.featured DESC', only: :index
default_scope order: 'pins.created_at DESC'
But that didn't fly...
UPDATE
I updated my model to define a scope:
scope :featy, order: 'pins.featured DESC'
default_scope order: 'pins.created_at DESC'
And updated my pins view to:
<%= render @pins.featy %>
However, now when I open my pins view, I get the error:
undefined method `featy' for #<Array:0x00000100ddbc78>
UPDATE 2
User.rb
class User < ActiveRecord::Base
attr_accessible :name, :email, :username, :password, :password_confirmation, :avatar,
:password_reset_token, :password_reset_sent_at
has_secure_password
has_many :pins, dependent: :destroy #destroys user posts when user is destroyed
# has_many :featured_pins, order: 'featured DESC', class_name: "Pin", source: :pin
has_attached_file :avatar, :styles => { :medium => "300x300#", :thumb => "120x120#" }
before_save { |user| user.email = user.email.downcase }
before_save { |user| user.username = user.username.downcase }
before_save :create_remember_token
before_save :capitalize_name
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
VALID_USERNAME_REGEX = /^[A-Za-z0-9]+(?:[_][A-Za-z0-9]+)*$/
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
validates :username, presence: true, format: { with: VALID_USERNAME_REGEX }, uniqueness: { case_sensitive: false }
validates :password, length: { minimum: 6 }, on: :create #on create, because was causing erros on pw_reset
Pin.rb
class Pin < ActiveRecord::Base
attr_accessible :content, :title, :privacy, :date, :dark, :bright,
:fragmented, :hashtag, :emotion, :user_id, :imagesource, :imageowner, :featured
belongs_to :user
before_save :capitalize_title
before_validation :generate_slug
validates :content, presence: true, length: { maximum: 8000 }
validates :title, presence: true, length: { maximum: 24 }
validates :imagesource, presence: { message: "Please search and choose an image" }, length: { maximum: 255 }
validates_inclusion_of :privacy, :in => [true, false]
validates :slug, uniqueness: true, presence: true,
exclusion: {in: %w[signup signin signout home info privacy]}
# for sorting featured and newest posts first
default_scope order: 'pins.created_at DESC'
scope :featured_order, order: 'pins.featured DESC'
def to_param
slug # or "#{id}-#{name}".parameterize
end
def generate_slug # makes the url slug address bar freindly
self.slug ||= loop do
random_token = Digest::MD5.hexdigest(Time.zone.now.to_s + title)[0..9]+"-"+"#{title}".parameterize
break random_token unless Pin.where(slug: random_token).exists?
end
end
protected
def capitalize_title
self.title = title.split.map(&:capitalize).join(' ')
end
end
users_controller.rb
class UsersController < ApplicationController
before_filter :signed_in_user, only: [:edit, :update, :show]
before_filter :correct_user, only: [:edit, :update, :show]
before_filter :admin_user, only: :destroy
def index
if !current_user.admin?
redirect_to root_path
end
end
def menu
@user = current_user
end
def show
@user = User.find(params[:id])
@pins = @user.pins
current_user.touch(:last_log_in) #sets the last log in time
if
[email protected]?
render 'pages/info/'
end
end
def new
@user = User.new
end
pins_controller.rb
class PinsController < ApplicationController
before_filter :signed_in_user, except: [:show]
# GET /pins, GET /pins.json
def index #Live Feed
@pins = Pin.all
@featured_pins = Pin.featured_order
respond_to do |format|
format.html # index.html.erb
format.json { render json: @pins }
end
end
# GET /pins, GET /pins.json
def show #single Pin View
@pin = Pin.find_by_slug!(params[:id])
require 'uri' #this gets the photo's id from the stored uri
@image_id = URI(@pin.imagesource).path.split('/').second
if @pin.privacy == true #check for private pins
if signed_in?
if @pin.user_id == current_user.id
respond_to do |format|
format.html # show.html.erb
format.json { render json: @pin }
end
else
redirect_to home_path, notice: "Prohibited 1"
end
else
redirect_to home_path, notice: "Prohibited 2"
end
else
respond_to do |format|
format.html # show.html.erb
format.json { render json: @pin }
end
end
end
# GET /pins, GET /pins.json
def new
@pin = current_user.pins.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @pin }
end
end
# GET /pins/1/edit
def edit
@pin = current_user.pins.find_by_slug!(params[:id])
end
Finally, on my index.html.erb I have:
<%= render @featured_pins %>