如何将DB中的记录保存为文本而不是id

时间:2016-12-20 14:08:21

标签: ruby-on-rails ruby

我正在做饭订餐系统,我是Rails的新手,我想知道如何在DB中保存记录(从餐馆订购的餐点)作为文本。例如,我在 db / seed.rb 中有一份餐馆和餐点列表,当我在订单中查看餐馆的餐点时查看订购的餐点(餐点名称和名称)餐厅)我看到餐厅的id和用餐的id。我知道这很容易解决,但我不知道去做这件事。

let data = {
    name: 'Somebody',
    address: {
        city: 'somewhere'
    },
    tags: [{
        id: 5
        name: 'new'
    }]
}
check(data, 'where') // should be true

db/seed.rb Restaurant.create(name: "PizzaHut") Restaurant.create(name: "Salad Story") Restaurant.create(name: "KFC") Meal.create(name: "PizzaHut Special - 32cm", restaurant_id: Restaurant.find_by(name: "PizzaHut").id) Meal.create(name: "PizzaHut Special - 42cm", restaurant_id: Restaurant.find_by(name: "PizzaHut").id) Meal.create(name: "Pepperoni - 32cm", restaurant_id: Restaurant.find_by(name: "PizzaHut").id) Meal.create(name: "Pepperoni - 42cm", restaurant_id: Restaurant.find_by(name: "PizzaHut").id) Meal.create(name: "Hawaii - 32cm", restaurant_id: Restaurant.find_by(name: "PizzaHut").id) Meal.create(name: "Hawaii - 42cm", restaurant_id: Restaurant.find_by(name: "PizzaHut").id) Meal.create(name: "Diablo - 32cm", restaurant_id: Restaurant.find_by(name: "PizzaHut").id) Meal.create(name: "Diablo - 42cm", restaurant_id: Restaurant.find_by(name: "PizzaHut").id) Meal.create(name: "Longer + Small Drink (Coca-Cola, Fanta, Sprite)", restaurant_id: Restaurant.find_by(name: "KFC").id) Meal.create(name: "Longer + Large Drink (Coca-Cola, Fanta, Sprite)", restaurant_id: Restaurant.find_by(name: "KFC").id) Meal.create(name: "Greek Salad - Medium", restaurant_id: Restaurant.find_by(name: "Salad Story").id) Meal.create(name: "Greek Salad - Large", restaurant_id: Restaurant.find_by(name: "Salad Story").id) Meal.create(name: "Salad Story Special - Medium", restaurant_id: Restaurant.find_by(name: "Salad Story").id) Meal.create(name: "Salad Story Special - Large", restaurant_id: Restaurant.find_by(name: "Salad Story").id)

staticpages/home.html.erb

<h1 class="jumbotron-font">Order your meal</h1> <div class="row"> <div class="col-md-6 col-md-offset-3"> <%= form_for(:orders, url: new_order_path) do |f| %> <%= f.label :restaurant, "Restaurant" %> <%= f.collection_select :restaurant_id, Restaurant.order(:name), :id, :name, {prompt: "Select a restaurant"}, {class: "form-control"} %> <%= f.label :meal, "Choose your meal" %> <%= f.grouped_collection_select :meal_id, Restaurant.order(:name), :meals, :name, :id, :name, {prompt: "Select a meal"}, {class: "form-control"} %> <%= f.label :suggestions, "Suggestions" %> <%= f.text_area :suggestions, rows: "4", placeholder: "e.g. I'd like to change sweetcorn for olives..." %> <%= f.submit "Place your order", class: "btn btn-primary" %> <% end %> </div> </div>

db/schema.rb

我的日志:

 ActiveRecord::Schema.define(version: 20161220125950) do

   create_table "meals", force: :cascade do |t|
     t.string   "name"
     t.integer  "restaurant_id"
     t.datetime "created_at",    null: false
     t.datetime "updated_at",    null: false
     t.index ["restaurant_id"], name: "index_meals_on_restaurant_id"
   end

   create_table "orders", force: :cascade do |t|
     t.string   "restaurant"
     t.string   "meal"
     t.text     "suggestions"
     t.integer  "user_id"
     t.datetime "created_at",      null: false
     t.datetime "updated_at",      null: false
     t.string   "restaurant_id"
     t.string   "meal_id"
     t.index ["user_id", "created_at"], name: "index_orders_on_user_id_and_created_at"
     t.index ["user_id"], name: "index_orders_on_user_id"
   end

   create_table "restaurants", force: :cascade do |t|
     t.string   "name"
     t.datetime "created_at",      null: false
     t.datetime "updated_at",      null: false
   end

   create_table "users", force: :cascade do |t|
     t.string   "provider"
     t.string   "uid"
     t.string   "name"
     t.datetime "created_at", null: false
     t.datetime "updated_at", null: false
   end

 end

*更新*

 Parameters: {"utf8"=>"✓", "authenticity_token"=>"TVdB/YJ8W2fQD0XslA7KwlY2WHPPTSsI6Tx18CE1v6vF9zJLjM5nFd6lO/gSrPwsy/fCnra54TeAtxrwGyIGoQ==", "orders"=>{"restaurant_id"=>"1", "meal_id"=>"11", "suggestions"=>""}, "commit"=>"Place your order"}
   User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
    (0.1ms)  begin transaction
   CACHE (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
   Restaurant Load (0.1ms)  SELECT  "restaurants".* FROM "restaurants" WHERE "restaurants"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
   SQL (91.1ms)  INSERT INTO "orders" ("suggestions", "user_id", "created_at", "updated_at", "restaurant_id", "meal_id") VALUES (?, ?, ?, ?, ?, ?)  [["suggestions", ""], ["user_id", 1], ["created_at", 2016-12-20 14:05:13 UTC], ["updated_at", 2016-12-20 14:05:13 UTC], ["restaurant_id", "1"], ["meal_id", "11"]]
    (241.2ms)  commit transaction

从视图中我只有 class Order < ApplicationRecord belongs_to :user belongs_to :restaurant end class User < ApplicationRecord has_many :orders has_many :restaurants, through: :orders end class Meal < ApplicationRecord belongs_to :restaurant end class Restaurant < ApplicationRecord has_many :orders has_many :meals has_many :users, through: :orders end

order/index.html.erb

这是一个简单的应用程序,只是为了练习,所以我想做单页应用程序,而不是实际确认餐厅的订单。我希望能够下订单,然后在历史列表中查看所有订单。

1 个答案:

答案 0 :(得分:0)

我通常对选择字段使用f.association。 如果问题出在您的表单中,请在选择字段(主页)上尝试:

    class BMap extends PropertyMap<B, BDto> {
          @Override
          protected void configure() {
                using(new BToBDto()).map(source).setC(null);
                //Other mappings...
          }
   }

那或使用.pluck方法说你特意要显示名字:

<%= f.label :restaurant, "Restaurant" %>
       <%= f.association :restaurants, collection: Restaurant.order(:name), {prompt: "Select a restaurant"}, {class: "form-control"} %>

       <%= f.label :meal, "Choose your meal" %>
       <%= f.association :meals, collection: Restaurant.order(:name).meals, {prompt: "Select a meal"},
                                              {class: "form-control"} %>