简体   繁体   English

Rails:保存父记录后自动创建关联记录

[英]Rails: Autocreate associated records as soon as parent record is saved

I'm trying to automatically create records in a child table as soon as a parent record is saved but I'm stuck and need a help how to do this. 我正在尝试在保存父记录后自动在子表中创建记录,但是我遇到了麻烦,需要帮助来执行此操作。

So my table hierarchy of the Orgs is like Org->Brand->Restaurant (eg. Americana(Org) -> KFC(Brand) ->San Francisco(Restaurant)). 因此,我对组织的表层次结构类似于组织->品牌->餐厅(例如Americana(组织)-> KFC(品牌)->旧金山(餐厅))。

A Brand has many menus and Menu belongs to a Brand. 一个品牌有许多菜单,而菜单属于一个品牌。 Now I want to cascade menu for each Restaurant from the Brand's menu because I want to be manage price by the Restaurant. 现在,我想从品牌菜单中级联每个餐厅的菜单,因为我想由餐厅管理价格。 In order to do that, I created a 'local_menus' table which contains restaurant_id and menu_id so that I don't have to duplicate some data and changes in the local_menus will not be affected to its parent. 为了做到这一点,我创建了一个包含restaurant_id和menu_id的“ local_menus”表,这样我就不必重复某些数据,并且local_menus中的更改不会受到其父级的影响。

The question is how do I auto create records in 'local_menus' as soon as a parent menu is created. 问题是,一旦创建父菜单,如何在“ local_menus”中自动创建记录。 To be more specific, when I create a menuA, the same menu needs to be created automatically for all the restaurants under the brand. 更具体地说,当我创建一个menuA时,需要为该品牌下的所有餐厅自动创建相同的菜单。 Appreciate your support. 感谢您的支持。

MODELS: 楷模:

class LocalMenu < ActiveRecord::Base
  belongs_to :restaurant
  belongs_to :menu
end

class Menu < ActiveRecord::Base
    has_many :local_menus
    belongs_to :brand
end

class Restaurant < ActiveRecord::Base
    has_many :menus, through: :local_menus
end

TABLES: 表:

local_menus
    t.integer  "restaurant_id",  limit: 4
    t.integer  "menu_id",        limit: 4
    t.boolean  "active_status",    limit: 1
    t.boolean  "instock_status", limit: 1
    t.integer  "price",          limit: 4


menus
    t.integer  "restaurant_id",      limit: 4
    t.string   "name",               limit: 255
    t.integer  "price",              limit: 4
    t.integer  "brand_id",           limit: 4
    t.integer  "category_id",        limit: 4
    t.text     "description",        limit: 65535
    t.boolean  "active_status",      limit: 1
    t.date     "start_date"
    t.date     "end_date"

restaurants
    t.string   "name",          limit: 255
    t.string   "name_kana",     limit: 255
    t.integer  "price",         limit: 4
    t.boolean  "active_status", limit: 1
    t.integer  "brand_id",      limit: 4

One way to do this would be use an :after_create callback to create local menus for all the brand's restaurants. 一种方法是使用:after_create回调为所有品牌餐厅创建本地菜单。

class Menu < ActiveRecord::Base

  after_create: :build_local_menus_for_brand

  def build_local_menus_for_brand
    brand.restaurants.each do |restaurant|
      self.local_menus.create!(restaurant_id: restaurant.id)
    end
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM