Relationships on a non ID field

Hi Guys,

I am a total newbie to rails - so please go easy :slight_smile:

So I have 2 models Country and City - the Country Model will have a
‘name’ and a ‘code’ field (so a record would be

ID name code
1 United Kingdom UK

Then my City table would look something like

ID country_code city_code name
2 UK LON London

So I guess the question is can you use has_many :city which would form
the relationship based on the char code rather than a ID?

Regards

On Aug 10, 1:35 pm, Pete M. [email protected]
wrote:

Then my City table would look something like

ID country_code city_code name
2 UK LON London

So I guess the question is can you use has_many :city which would form
the relationship based on the char code rather than a ID?

has_many does have a primary_key option for this sort of thing. I
would more commonly have a country_id column on cities rather than a
country_code column

Fred

Pete M. wrote:

So I have 2 models Country and City - the Country Model will have a
‘name’ and a ‘code’ field (so a record would be

ID name code
1 United Kingdom UK

Then my City table would look something like

ID country_code city_code name
2 UK LON London

Country
ID name code
1 United Kingdom UK

City
ID country_id city_code name
2 1 LON London

Country
has_many :cities

City
belongs_to :country