ruby-on-rails - 有没有办法通过对 id 的搜索求和来计算价格
问题描述
我是 Rails 世界的初学者,甚至是 Ruby 世界的初学者,所以请耐心等待...
所以我有这个数据库架构:
ActiveRecord::Schema.define(version: 20201130233206) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "orders", force: :cascade do |t|
t.integer "total"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "store_id"
t.integer "product_id"
t.index ["product_id"], name: "fki_productid_fkey", using: :btree
t.index ["store_id"], name: "fki_store_fk", using: :btree
end
create_table "products", force: :cascade do |t|
t.integer "store_id"
t.string "name"
t.string "sku"
t.string "tipo"
t.integer "price"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["store_id"], name: "index_products_on_store_id", using: :btree
end
create_table "stores", force: :cascade do |t|
t.string "name"
t.text "address"
t.string "email", default: "francisco.abalan@pjchile.com"
t.string "phone"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_foreign_key "orders", "products", name: "productid_fkey"
add_foreign_key "orders", "stores", name: "store_fk"
add_foreign_key "products", "stores"
end
这个想法是:订单有太多的产品和一对二的商店。因此,一个订单,许多产品,1 个商店。同样的逻辑适用于产品和商店。
我用这个种子填充了这个数据库:
store = Store.create([{id: 1, name: "La reina", address: "Irarrazaval", phone: "12232332"}, {id: 2, name: "La florida", address: "Walker Martinez", phone: "12332"} ])
Product.create([{name: "Pizza de la reina", store: store.first}, {name: "Pizza de la florida", store: store.second}])
我如何编码&我在哪里编码计算的顺序应该是这样的(至少在我看来是这样):
class OrderController < ApplicationController
def index
@products = Product.find([:first_product, :second_product]).sum
@order = Order.find(params([:id]))
render json: @order + @products
end
再次,我非常抱歉如此含糊,所以如果你想上传更多,我会很快完成。
编辑 1:模型:order.rb
class Order < ApplicationRecord
has_one :store, optional: true
has_many :product, optional: true
end
产品.rb
class Product < ApplicationRecord
belongs_to :store, optional: true
belongs_to :order, optional: true
end
商店.rb
class Store < ApplicationRecord
has_many :product
end
控制器:
order_controller.rb
class OrderController < ApplicationController
def index
@products = Product.find([:first_product, :second_product]).sum
@order = Order.find(params([:id]))
render json: @order + @products
end
def new
@order = Order.new
end
def show
@order = Order.find(params[:id])
end
def sum
@products = Product.find(params[:first_product, :second_product]).sum
@order = Order.find(params([:id]))
render json: @order + @products
end
private
def orders_params
#Brings store table and asks for params
params.permit(:id, :first_product, :second_product, :store_id);
end
end
product_controller.rb
class ProductController < ApplicationController
def index
@product = Product.all
render json: @product
end
def new
@product = Product.new
end
def create
@product = Product.new(product_params)
if @product.save!
render json: @product
end
end
def show
@product = Product.find(params[:id])
end
def edit
@product = Product.find(params[:id])
end
def update
@product = Product.find(params[:id])
if @product.update(product_params)
render json: @product
else
render json: @product
end
end
def destroy
@product = Product.find(params[:id])
@product.destroy
render json: @product
end
private
def product_params
#Brings product table and asks for params
params.permit(:name, :sku, :tipo, :price, :store_id);
end
end
store_controller.rb
class StoreController < ApplicationController
def index
@store = Store.all
render json: @store
end
def new
@store = Store.new
end
def create
@store = Store.new(store_params)
if @store.save!
render json: @store
end
end
def show
@store = Store.find(params[:id])
end
def edit
@store = Store.find(params[:id])
end
def update
@store = Store.find(params[:id])
if @store.update(store_params)
render json: @store
else
render 'edit'
end
end
def destroy
@store = Store.find(params[:id])
@store.destroy
render json: @store
end
private
def store_params
#Brings store table and asks for params
params.permit(:name, :address, :email, :phone);
end
end
所以,我挺过来了。首先,我的模特和人际关系无处不在。我还缺少订单和产品之间的中间表。之后,我的 sum 方法发生了一些变化,但想法就在那里。非常感谢所有在这里试图帮助我的人。我还要感谢哈利,这位神秘的不和谐朋友指导我完成了一切,并以极大的耐心帮助了我。对你来说,哈利,你值得拥有一切<3
解决方案
我想在下面的方法中,您想找到所有符合某些条件的产品并计算总价
def sum
@products = Product.find(params[:first_product, :second_product]).sum
@order = Order.find(params([:id]))
render json: @order + @products
end
我会这样尝试:
Product.where(id: [first_product.id, second_product.id]).sum(:price)
只需使用where
方法而不是find
使用 id 数组 :)
- update1:我没有检查其余的代码....
- 更新2:请不要忘记,使用整数作为价格意味着实际上这将以美分定价 - 所以我会将列重命名为 price_cents :)
推荐阅读
- image - 如何将 .jpg 读入 tensorflow 数据集并使用会话显示图像
- ssl - Firefox 中格式错误的服务器 Hello(TLS1.3)
- javascript - 属性名称未知时如何在javascript中解析JSON数据
- azure - Azure DevOps - ARM 部署 - 密钥保管库和托管标识
- python - Python 3:运算符 -IndentationError
- sql - 视图和触发器的上次运行日期
- apache-kafka-streams - Kafka Streams 将新源添加到正在运行的应用程序
- nginx - 通过 Nginx 反向代理 .net 核心不起作用
- react-native - import * as SQLite from 'expo-sqlite' not working
- ignite - 点燃近缓存 - 可以将相同的分布式缓存配置为一个点燃客户端中的近缓存和另一个点燃客户端中的常规缓存吗?