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: def create @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 foreign key) 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? 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. What is a better approach to fixing this? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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: > > def create > @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 > foreign key) > > 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 do.> 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 class User def artist? self.videos.size > 0 end end> > What is a better approach to fixing this?I don''t think it''s broken. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hmmm, now that I think about it. Yes, you are right... On the side. Marnen, would you keep this schema as is, or would you modify it, in any way? Perhaps, as I was suggesting about having the User model as an STI? Or even polymorphic? What are your thoughts? On Oct 7, 4:33 am, Marnen Laibow-Koser <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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: > > > def create > > @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 > > foreign key) > > > 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 > do. > > > 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 > class User > def artist? > self.videos.size > 0 > end > end > > > > > What is a better approach to fixing this? > > I don''t think it''s broken. > > Best, > -- > Marnen Laibow-Koserhttp://www.marnen.org > mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.