我创建了一个功能强大的电子商务平台,会员可以在这里买书。一切正常,除非通常在会员购买书籍之后,我想展示即将推出的书籍,以及他在一页上购买的书籍。
例如,如果索引页面显示共10本图书(5本图书可用,5本即将推出)。
让我们说一个会员购买3即将推出的书籍。
我希望页面显示(5本书可用,2本ComingSoon,以及他购买的3本书)= 10总计
目前,我已在我的图书模型中定义了图书可用性。有没有办法减去两个阵列,以便我能够得到他没有买过的2即将到来的书?
(5即将推出的书籍 - 他买过的3本书)= 2即将推出
EX:
5 Available Books {1, 2, 3, 4, 5}
5 Coming Soon Books {6, 7, 8, 9, 10}
3 Books Purchased {6, 8, 9}
I need to subtract 5 Coming Soon - 3 Books Purchased giving me
2 Coming Soon Books Left {7, 10}
Then I can Display 5 Available Books, 3 Books Purchased, and 2 Coming Soon
这是我的代码,任何帮助都会非常感激。
模型
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :member
has_many :books, through: :orders
has_many :orders, :dependent => :destroy
end
class Order < ActiveRecord::Base
attr_accessible :book_id, :order_date, :user_id
belongs_to :user
belongs_to :book
end
class Book < ActiveRecord::Base
attr_accessible :description, :pages, :title, :available_in, :price, :release_date
belongs_to :publisher
has_many :orders
has_many :users, through: :orders
###Available Book
def book_is_available
self.release_date < Date.today
end
###Coming Soon or Not Available
def book_is_not_available(issue)
if Date.today < self.release_date
book.id
end
end
end
CONTROLLER
class BooksController < ApplicationController
def index
@books = Book.all
@orders = current_user.orders
respond_to do |format|
format.html
format.json { render :json => @issues }
end
end
end
SCHEMA
create_table "users", :force => true do |t|
t.string "name"
t.string "email"
t.boolean "member", :default => false ###Becomes Member on SignUp
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "orders", :force => true do |t| ###Created when Purchased a BOOK
t.integer "user_id"
t.integer "book_id"
t.date "order_date"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "books", :force => true do |t|
t.integer "publisher_id"
t.string "title"
t.string "pages"
t.text "description"
t.decimal "price", :precision => 8, :scale => 2
t.date "release_date"
t.datetime "created_at", :null => false
t.datetime "updated_at",
end
视图
<% if current_user.member? %>
<% @books.each do |book| %>
###Shows All Available Books
<% if book.book_is_available %>
<%= book.title %>
<% end %>
###Shows Books Bought By Member
<% @orders.each do |order| %>
<% if order.book.id == book.id %>
<%= book.title %>
<% end %>
<% end %>
<% end %> ###I want to Subtract/Remove the Above Books from the Books Below
###Shows All Books Not Available or Coming Soon
<% @books.each do |book| %>
<%= book.book_is_not_available(book) %>
<% end %>
<% end %>
答案 0 :(得分:1)
这应该有用。
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :member
has_many :books, through: :orders
has_many :orders, :dependent => :destroy
def purchased_books
books
end
def unavailable_unpurchased_books
Book.unavailable.where("id Not IN ?",books.pluck(:book_id))
end
end
class Book < ActiveRecord::Base
attr_accessible :description, :pages, :title, :available_in, :price, :release_date
has_many :orders
has_many :users, through: :orders
scope :unavailable,->{where("release_date > ?",Date.today)}
scope :available,->{where("release_date < ?",Date.today)
end
在控制器中
def index
@available_books = Book.available
@orders = current_user.orders
respond_to do |format|
format.html
format.json { render :json => @issues }
end
end
在视图中
<% if current_user.member? %>
###Shows All Available Books
<% @available_books.each do |book| %>
<%= book.title %>
<% end %>
###Shows Books Bought By Member
<% @orders.each do |order| %>
<%= order.book.title %>
<% end %>
###Shows All Books Not Available or Coming Soon
<% current_user.unavailable_unpurchased_books do |book| %>
<%= book.title %>
<% end %>
<% end %>
使用类范围非常有用,可以将查询unurchased_books变为SELECT * FROM books WHERE release_date > '2014-05-14' AND id Not IN (books the user purchased)