Primary key?

can you use 2 pirmary keys.

the standard user_id key exists but i would also like to use another
key for my links. i am not sure how to implement this. also i don’t
want any auto incrementing of this second primary key that i would
like to use for my relationships.

if there is a good db, model, relationship tutorial out there i would
love to know about it.

are you mixing up “primary keys” and foreign keys" ?

Every Rails model has, by default, an auto-incrementing “id” column as
primary key, and columns like “user_id” are no primary keys, but
foreign keys to link to related objects/rows in other tables. those
are not auto-incrementing and not primary. and you can have as many od
them as you want.

anyways, if your really need 2 primary keys in a model, try this:

yes, i think i am mixing up primary, foreign

so do i declare
belongs_to :user foreign_key “my_id”

model (user), my_id, name, country_id
model (movies), also has my_id, movie_id
model (country), country details

i need to output the name of the person that has a given movie wihout
having to use nested finds.

so i need output thru the movie model: name of user and then find
details about the country as well.

i did look at this earlier.

It depends on the association type. Every table has an id on it
automatically (hopefully).

say you have users and groups.

a group has many users

table name users, has a column called group_id
class User < AR
belongs_to :group

table name groups
class Group < AR
has_many :users

if you wanted to spell out the foreign key, you’d put has_many :users,
foreign_key :group_id. The belongs_to will be belongs_to :group,
foreign_key :user_id



This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs