I have a users table(below) and now i want to create a new table
“occupation” so the users can fill in there occupation and store it in
the occupation-table.
Question: how can i realize a realionship between the users-table and
the occupation-table?
I have a users table(below) and now i want to create a new table
“occupation” so the users can fill in there occupation and store it in
the occupation-table.
Question: how can i realize a realionship between the users-table and
the occupation-table?
Grtz…remco
Hi,
If a user is having many occupations
then you can give like
has_many :occupations in CreateUsers
and
belongs_to :occupation,
:class_name => “UserOccupation”,
:foreign_key => “user_occupation_id”
I have a users table(below) and now i want to create a new table
“occupation” so the users can fill in there occupation and store it in
the occupation-table.
Question: how can i realize a realionship between the users-table and
the occupation-table?
Grtz…remco
Hi,
If a user is having many occupations
then you can give like
has_many :occupations in CreateUsers
and
belongs_to :occupation,
:class_name => “UserOccupation”,
:foreign_key => “user_occupation_id”
in Occupation table
Hi,
Thanks for you’re quick reponse…
But must i add a column user_occupation_id in the users-table?
But must i add a column user_occupation_id in the users-table?
Possibly. it depends on your data:
if a user has a single occupation then the user table needs an
occupation_id column
if an occupation belongs to a single user then the occupation table
needs a user_id column
if a user can have many occupations and an occupation can have many
users then you don’t need to add columns to either of those table,
but you do need to create a join table occupation_users with columns
user_id and occupation_id.
You then do
class User
has_many :occupation_users
has_many :occupations, :through => :occupation_users
end
and similarly for Occupation.
Question: how can i realize a realionship between the users-table and
t.column :created_at, :datetime
:class_name => "UserOccupation",
remco
Actually, if a user has many occupations, then the relationship will
need to be modeled as a has_and_belongs_to_many (HABTM) and you will
need to create a table called user_occupations_users table to model the
many-to-many relationship. Search for has_and_belongs_to_many (HABTM)
and you should be able to get enough to get started.
On the other hand, if a user can belong to only one occupation, then you
need to add a table called occupations and in your user table, you need
to occupation_id as an INT field. You can create a new migration that
adds the column to the table. Then, user belongs_to occupation and
occupation has_many users.
well i think
we can do it with a direct has_many relationship between Users and
occupations , no need of an through relationship if users has_many users
only…
But if occupation has many users then we have a many to any
relationship , in this case we will opt for through relationship .
DHH
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.