Hi,
I'm fairly new to RoR and I'm trying to figure out the best way to deal
with Users in my application. I've got (for the purpose of this thread)
3 models, User, Subject, and SubjectUserAssignment.
The user model is joined to the Subject model with the following,
creating a many to many relationship between User and Subject:
This works fine, I can return all users for a given subject, and all
subjects for a given user. The problem is that the Subject model has
user_id_created and user_id_updated fields, which contain the user_id of
the user(s) who created and updated the subject. So a user can either be
related to a subject because they're assigned to it, or because they
created/updated it, or both. How do I go about specifying this in my
User model? User.subjects should return all subjects assigned to a user,
but I need something else like User.created_subjects to show all
subjects created by a user. Am I using :foreign_key correctly in the
Subject model?
Hi,
I'm fairly new to RoR and I'm trying to figure out the best way to deal
with Users in my application. I've got (for the purpose of this thread)
3 models, User, Subject, and SubjectUserAssignment.
The user model is joined to the Subject model with the following,
creating a many to many relationship between User and Subject:
This works fine, I can return all users for a given subject, and all
subjects for a given user. The problem is that the Subject model has
user_id_created and user_id_updated fields, which contain the user_id of
the user(s) who created and updated the subject. So a user can either be
related to a subject because they're assigned to it, or because they
created/updated it, or both. How do I go about specifying this in my
User model? User.subjects should return all subjects assigned to a user,
but I need something else like User.created_subjects to show all
subjects created by a user. Am I using :foreign_key correctly in the
Subject model?
You're using :foreign_key correctly, but you're not using belongs_to
correctly. Remember that the first argument to belongs_to is the name
of the *association*, not necessarily the class or table. So right now,
you're creating two different associations called user (presumably one
is overwriting the other). What you want is something like
belongs_to :creator, :foreign_key => 'user_id_created', :class_name =>
'User'
belongs_to :updater, :foreign_key => 'user_id_updated', :class_name =>
'User'
Please refer to the belongs_to rdoc for more info on how this all works.
Hi,
I'm fairly new to RoR and I'm trying to figure out the best way to deal
with Users in my application. I've got (for the purpose of this thread)
3 models, User, Subject, and SubjectUserAssignment.
The user model is joined to the Subject model with the following,
creating a many to many relationship between User and Subject:
This works fine, I can return all users for a given subject, and all
subjects for a given user. The problem is that the Subject model has
user_id_created and user_id_updated fields, which contain the user_id of
the user(s) who created and updated the subject. So a user can either be
related to a subject because they're assigned to it, or because they
created/updated it, or both. How do I go about specifying this in my
User model? User.subjects should return all subjects assigned to a user,
but I need something else like User.created_subjects to show all
subjects created by a user. Am I using :foreign_key correctly in the
Subject model?
First, unless you fear naming conflicts I think and Assignment model
would stand on it's own so I'll present my suggestion based on that.