Ruby on rails Rails-如何分配has“u和”u属“u多之间”;商铺";及;“购物中心”;(在控制器和窗体视图中)
我正在制作一个简单的rails应用程序,作为科威特所有商场和每个商场内商店的目录。我创建了一个商店模型和购物中心模型,并进行了以下关联:-Ruby on rails Rails-如何分配has“u和”u属“u多之间”;商铺";及;“购物中心”;(在控制器和窗体视图中),ruby-on-rails,has-and-belongs-to-many,Ruby On Rails,Has And Belongs To Many,我正在制作一个简单的rails应用程序,作为科威特所有商场和每个商场内商店的目录。我创建了一个商店模型和购物中心模型,并进行了以下关联:- class Shop < ActiveRecord::Base has_and_belongs_to_many :malls end class Mall < ActiveRecord::Base has_and_belongs_to_many :shops end 这就是我被困的地方: 我应该在控制器中放置什么,以便在创建(或更新)
class Shop < ActiveRecord::Base
has_and_belongs_to_many :malls
end
class Mall < ActiveRecord::Base
has_and_belongs_to_many :shops
end
这就是我被困的地方:
您有两个选项可以将一些
商店
添加到购物中心
:
-这将为每个添加的店铺创建中间关系:
mall = Mall.first
mall.shops << Shop.find(1)
mall.shops << Shop.find(2)
了解如何在视图中控制从模型创建和更新
mall = Mall.first
mall.shops << Shop.find(1)
mall.shops << Shop.find(2)
mall = Mall.first
mall.shops_ids = [2,3]