我是一个菜鸟,所以我提前为这个问题缺乏清晰度和/或简单性而道歉。
我正尝试以4种方式对产品index.html.erb中的产品进行排序。其中三个正在运作,但“趋势”功能却没有。我无法通过clock.size订购(这可能与表或时钟和产品之间的关联有关 - 我显然不确定)
任何帮助都会很棒!感谢。
的routes.rb
Kijovo::Application.routes.draw do
root "welcome#index"
get "products/filter/:scope" => "products#index", as: :filtered_products
resources :products do
resources :clocks
end
end
products_controller.rb
class ProductsController < ApplicationController
before_action :require_signin
before_action :require_admin, except: [:index, :show]
before_action :set_product, only: [:show, :edit, :update, :destroy]
def index
@product = Product.find_by(params[:id])
case params[:scope]
when 'past'
@products = Product.past
when 'upcoming'
@products = Product.upcoming
when 'tba'
@products = Product.tba
else
@products = Product.trending
end
end
product.rb(产品型号)
class Product < ActiveRecord::Base
has_many :clocks, dependent: :destroy
has_many :clockers, through: :clocks, source: :user
before_validation :generate_slug
validates :slug, uniqueness: true
# scope :trending, -> { where("releasing_on >= ?", Time.now).order("clocks.size") }
scope :trending, -> { joins(:clocks).where("releasing_on >= ?", Time.now).order("clocks.size") }
scope :upcoming, -> { where("releasing_on >= ?", Time.now).order("releasing_on").order(:name) }
scope :past, -> { where("releasing_on <= ?", Time.now).order("releasing_on desc").order(:name) }
scope :tba, -> { where(releasing_on: nil).order(:name) }
end
products_helper.rb
module ProductsHelper
def format_price(product)
if product.tbd?
"TBD"
else
number_to_currency(product.price)
end
end
def format_releasing_on(product)
if product.tba?
"TBA"
else
product.releasing_on
end
end
def format_releasing_on_remaining(product)
if product.tba?
""
elsif product.out?
"OUT"
else
(product.releasing_on - DateTime.now).to_i
end
end
end
产品 - index.html.erb
<header>
<div class="row">
<div class="span12 centered-pills">
<nav>
<ul class="nav nav-pills">
<li><%= link_to 'Trending', filtered_products_path(:trending) %></li>
<li><%= link_to 'Releasing Soon', filtered_products_path(:upcoming) %></li>
<li><%= link_to 'Just Released', filtered_products_path(:past) %></li>
<li><%= link_to 'TBA', filtered_products_path(:tba) %></li>
</ul>
</nav>
</div>
</div>
</header>
clock.rb(时钟模型)
class Clock < ActiveRecord::Base
belongs_to :product
belongs_to :user
end
时钟表架构
create_table "clocks", force: true do |t|
t.integer "product_id"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
产品表架构
create_table "products", force: true do |t|
t.string "name"
t.text "description"
t.decimal "price"
t.datetime "created_at"
t.datetime "updated_at"
t.date "releasing_on"
t.string "website"
t.string "image_file_name"
t.string "industry"
t.string "slug"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
t.string "company"
t.integer "total_clocks"
end
SQL In Rails控制台失败:
2.0.0-p576 :037 > Product.joins(:clocks).where("releasing_on >= ?", Time.now).order("clocks.size")
Product Load (0.3ms) SELECT "products".* FROM "products" INNER JOIN "clocks" ON "clocks"."product_id" = "products"."id" WHERE (releasing_on >= '2014-11-07 18:55:15.595051') ORDER BY clocks.size
SQLite3::SQLException: no such column: clocks.size: SELECT "products".* FROM "products" INNER JOIN "clocks" ON "clocks"."product_id" = "products"."id" WHERE (releasing_on >= '2014-11-07 18:55:15.595051') ORDER BY clocks.size
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: clocks.size: SELECT "products".* FROM "products" INNER JOIN "clocks" ON "clocks"."product_id" = "products"."id" WHERE (releasing_on >= '2014-11-07 18:55:15.595051') ORDER BY clocks.size
答案 0 :(得分:0)
由于您希望按clocks
排序,您必须在语句中加入该表,例如:
class Product < ActiveRecord::Base
has_many :clocks
# ...
scope :trending, -> { joins(:clocks).group("#{Product.table_name}.id").where("releasing_on >= ?", Time.now).order("count(clocks.product_id)") }
end