Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 连接两个轨道模型_Ruby On Rails - Fatal编程技术网

Ruby on rails 连接两个轨道模型

Ruby on rails 连接两个轨道模型,ruby-on-rails,Ruby On Rails,所以我有一个叫做城市的模型和一个叫做商店的模型。我想连接我的商店和城市模型,以便在需要时能够访问商店的城市(例如@store.city)。我该怎么做呢 我假设在商店模型中需要一个has\u many:cities标签,在城市模型中需要一个belown\u to:store标签。我有点迷路了。非常感谢您的帮助。第一个警告是。除非有意这样做,否则在RoR中使用复数作为型号名称是非常规的。因此,您的存储应该是存储。同样,对于城市 我猜你想在一个城市有很多商店的地方建立一种关系。在这种情况下,您可能希望

所以我有一个叫做城市的模型和一个叫做商店的模型。我想连接我的商店和城市模型,以便在需要时能够访问商店的城市(例如@store.city)。我该怎么做呢


我假设在商店模型中需要一个has\u many:cities标签,在城市模型中需要一个belown\u to:store标签。我有点迷路了。非常感谢您的帮助。

第一个警告是。除非有意这样做,否则在RoR中使用复数作为型号名称是非常规的。因此,您的
存储
应该是
存储
。同样,对于城市

我猜你想在一个城市有很多商店的地方建立一种关系。在这种情况下,您可能希望商店中有一个外键,表示该商店属于某个城市

rails generate model City
rails generate model Store
在您的商店模型中,您要添加

belongs_to :city
has_many :stores
在城市模型中,要添加

belongs_to :city
has_many :stores
更新

确保在存储表中有一个名为
city\u id

答案


“外键怎么办?”

您只需将其添加到表中即可。Rails将自动为您建立此连接。更准确地说。在
存储
表迁移中,确保

.....
t.integer :city_id
.....

如果每家门店仅位于一个城市:

首先,确保stores表有一个
city\u id
字段

您的商店模型将包含

belongs_to :city
而城市模式将具有

has_many :stores
这将允许您访问城市中的所有商店:

@city = City.find_by_id(id)
@city.stores #gives an array of stores belonging to that city.
您还可以获取特定商店的城市信息:

@store = Store.find_by_id(id)
@store.city #gives the record for this store's city, this is based on the city_id field in your stores table

如果你的商店是连锁店,并且有可能在许多城市,那么你将需要设置一些不同的东西。您将需要城市和商店之间的另一张表,以实现“多对多”连接。此表可以称为
city\u stores
,其中每个记录将包含一个store\u id和一个city\u id

那么您的型号也会有一些变化:

model Store < ActiveRecord::Base
  has_many :city_stores
  has_many :cities, :through => :city_stores
end

model City < ActiveRecord::Base
  has_many :city_stores
  has_many :stores, :through => city_stores
end

@store.cities #now gives a list of all the cities this store resides in
@city.stores #still gives a list of all stores in the city
modelstore:城市商店
终止
模型城市城市商店
终止
@store.cities#现在列出该商店所在的所有城市
@city.stores仍然提供了该城市所有商店的列表

您应该检查本指南,并在您的店铺型号中添加“是”:

所属城市

你有一个:

city\u id
在您的
stores
表中,您可以这样做,您还需要以另一种方式进行关联:

在您的城市模型中:

有很多:商店

现在,您将能够执行以下操作

store = Store.find(1)
store.city.name # => this will return the `name` field of the associated city
你也可以这样做:

city = City.find(1)
city.stores # => this will return an array of the stores on the selected City

一个城市里有很多商店吗?这是一个需要花费时间才能回答的问题。如果您接受对您过去提出的更多问题的答案,您可能会更幸运地找到人进行时间投资。外键怎么办?@TopChef..只需确保您的
存储
表包含一列
城市id
,然后Rails将按照惯例理解它是外键。否则,您必须在关系中明确地提到外键的名称,比如
has\u many:stores,:foreign\u key=>“cityId”
我应该将外键设为int吗?我正在尝试将一列迁移到现有表中,但不知道将其指定为什么。我目前让rails生成迁移,将城市id添加到商店。通过
id
add列:stores,:city\u id,:integer
,rails关联应该是
integer
,然后在你的
新店
表单中,你可以为城市添加一个选择,比如: