Christian Fazzini wrote:
Ok, Ive got the following models:User, Artist and Video
User is 1:1 with Artist (A user can register to be an artist)
Artist is 1:M with Video (An artist can have one or many videos)
Note that, before any user can register as an artist. He/she must
already have an account with the system. i.e. He must be a registered
user first. Hence the 1:1 relationship with User and Artist.
Here is the problem, when an artist tries to upload a video:
@video = Video.new(params[:video])
@video.artist = current_user.artist
For this particular user:
In the User model, he has id => 29
In the Artist model, he has id => 1, user_id => 29 (user_id being the
This means, when the video record is saved to the db. It is saved as:
id => 1, artist_id => 1 (instead of 29), title => 'foobar', etc
How can such be fixed?
There's nothing to fix. artist_id is giving the ID of the artist
record, not the user record. This is exactly what you've asked Rails to
Should I remodel my schema? I was thinking of
just having one User model and have this as an STI. Member (Registered
users) and Artist models would inherit from the User model.
That might work, or just have a role field in your User model, and only
role "artist" can upload videos.
Or get rid of the distinction altogether, and do something like
self.videos.size > 0
What is a better approach to fixing this?
I don't think it's broken.