So I have a model called cities and a model called stores. I would like to connect my stores and cities model so that I will be able to call on the store's city if needed (eg @Store.city). How would I go about doing so?
I'm assuming that I need a has_many :cities tag in the store model and a belongs_to :store tag in the cities model. I get kind of lost past that point. Any help would be very much appreciated?
First warning is that. Unless intended to, it is unconventional to use plural for model name in RoR. So your Stores
should be Store
. Similarly, for city.
I am guessing that you want to have a relationship where a city has_many stores. In this case, You would want a foreign key in your store signifying that the store belongs_to a city.
rails generate model City
rails generate model Store
in your Store model, you want to add
belongs_to :city
in your city model, you want to add
has_many :stores
Update
Make sure that in your your store table, you have a foregin key named city_id
Answers
"What do I do with the foreign key?"
You only have to add it to the table. Rails will automatically make this connection for you. To be more precise. In your stores
table migration, make sure you have
.....
t.integer :city_id
.....
If each store is in only one city:
First, make sure that your stores table has a city_id
field.
your store model will contain a
belongs_to :city
and the city model will have
has_many :stores
This will allow you to access all the stores in a city:
@city = City.find_by_id(id)
@city.stores #gives an array of stores belonging to that city.
You can also get the city for a particular store:
@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
If your stores are chain stores, and have the potential of being in many cities, then you will need to set things up a little differently. You will need another table between cities and stores for a "many to many" connection. This table could be called city_stores
where each record would contain one store_id and one city_id.
Then your models change a bit also:
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
You should check this guide A Guide to Active Record Associations and yes once you have in your Store model:
belongs_to :city
and you have a:
city_id
on your stores
table, you will be able to do that and you also need to do the association on the other way:
in you City model:
has_many :stores
now you will be Able to do something like
store = Store.find(1)
store.city.name # => this will return the `name` field of the associated city
and you can do this too:
city = City.find(1)
city.stores # => this will return an array of the stores on the selected City
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.