如何处理一个用户的多个订单 [英] How to handle multiple orders for an user

查看:34
本文介绍了如何处理一个用户的多个订单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来有这样的查询..

In my view there is a query like this ..

<%= CaricatureType.find_by(id: StudioOrder.find_by(user_id: spree_current_user.id).style_id).name %>

studio_orders 和 caricature_types 的模式如下

The schema for studio_orders and caricature_types is as follows

create_table "studio_orders", force: :cascade do |t|
    t.integer  "user_id"
    t.integer  "occasion_id"
    t.integer  "style_id"
    t.integer  "number_of_people"
    t.string   "artwork_size"
    t.integer  "package_id"
    t.text     "instructions"
    t.datetime "created_at",         null: false
    t.datetime "updated_at",         null: false
    t.float    "initial_price"
    t.float    "final_price"
    t.string   "image_file_name"
    t.string   "image_content_type"
    t.integer  "image_file_size"
    t.datetime "image_updated_at"
    t.json     "avatars"
    t.integer  "address_id"
  end


create_table "caricature_types", force: :cascade do |t|
    t.string   "name"
    t.text     "description"
    t.float    "price"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
  end

现在的问题是,我上面发布的视图中的查询出现在 order_summary 屏幕中,查询只是输出他选择的样式的名称.现在的问题是,从技术上讲,studio_orders 中可以有很多行具有相同的用户 ID. 但是如何在 order_summary 屏幕中显示用户选择的当前样式.

Now the question is, the query in the view I have posted above comes in order_summary screen, and the query simply outputs the name of the style he has choosen. Now the thing is, technically there can be many rows in the studio_orders with the same user id. But how to display the current style the user has choosen in the order_summary screen.

推荐答案

any ActiveRecord 声明放在您的视图中是不好的做法.所有这些都应该由控制器处理:

It's bad practice to put any ActiveRecord declarations in your view. All of that should be handled by the controller:

#app/controllers/your_controller.rb
class YourController < ApplicationController
  def show
    @types = CaricatureType.find_by(id: StudioOrder.find_by(user_id: spree_current_user.id).style_id).name %> 
  end
end

这有两个好处:

  1. 所有逻辑都保存在一个地方
  2. 如果通过 @instance_variable 多次调用数据,
  3. ActiveRecord 将重用数据
  1. All your logic is kept in one place
  2. ActiveRecord will reuse the data if it's called multiple times through an @instance_variable

<小时>

对于您的具体问题,


For your specific issue,

但是如何在order_summary屏幕中显示用户选择的当前样式

But how to display the current style the user has choosen in the order_summary screen

这个由你来决定.

理想情况下,您希望在 studio_orders 表中放置一个 current 列.将 boolean 类型分配给此列将允许您使用以下内容:

Ideally, you'd want to put a current column in your studio_orders table. Assigning the type boolean to this column will allow you to use the following:

@current_type = CaricatureType.find_by id: spree_current_user.studio_orders.style_id, current: true

Rails 可能重视约定优于配置 - 但您可以自由构建架构/模型如你所见.这包括在需要时设置 current 列.

Rails might value convention over configuration - but you're free to construct your schema / models as you see fit. This includes setting a current column if you need it.

这篇关于如何处理一个用户的多个订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆