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)).
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.
The question is how do I auto create records in 'local_menus' as soon as a parent menu is created. To be more specific, when I create a menuA, the same menu needs to be created automatically for all the restaurants under the brand. 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
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire