I’m getting back into Ruby on Rails development after somewhat of a hiatus. I’m having trouble working out the proper Model associations and migrations for this simple practice app I’m working on. Basically, I have 3 models:
City, Marker and Category
City -> name:string, desc:text
Marker -> name:string, lat:decimal, lng:decimal
Category -> name:string
A city has multiple markers on a map. Each marker belongs to a category (i.e. Restaurant, Record Store etc). What I have at the moment is:
class City < ActiveRecord::Base
has_many :markers
end
class Marker < ActiveRecord::Base
has_one :category
belongs_to :city
end
class Category < ActiveRecord::Base
belongs_to :marker
end
I have a feeling I’ve got this wrong. Even if I haven’t, my real trouble is working out the migration. I thought doing something like:
rails generate migration Marker name:string lat:decimal lng:decimal city_id:integer
Would tie up the City/Marker models but it hasn’t worked out that way. I’m really quite new to all this and the associations has ground me to a halt. Any help would be appreciated!
Barry
I think you’re confusing model and migration generators.
If you want to generate a model:
If you want to generate a migration for this model: