Hey guys, I''m not that great with Rails yet, but I''m trying to get there. I''m developing a site where users will be able to upload pictures and videos (eventually). I want to set the pictures and the videos up as separate models, or at least so they will act like it. Comments, tags, and ratings will go onto the pictures and videos. I''ve decided the easiest way to do it, while keeping everything nice and DRY, is to create a "content_item" model, through which the pictures and videos will act. I''m trying to find a way to basically do the following: Picture.find(1) will go find the item with id = 1 in the content_items model. If the content_type at id 1 isn''t a picture, it will return an error. Video.find_by_username("Foobar") would return all videos for the user Foobar by using content_type "Video" I don''t think polymorphic associations applies to this, since the content doesn''t has_one video, the content IS the video, if that makes any sense. As of right now, I have a blank model. I know the answer is something simple, but I just can''t seem to find it. Thanks, Jonathon --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
What about single table inheritance? You can add two more models. One for Video and one for Picture and have them inherit from ContentItem. I have a User model in my app and two other models which use the same table: Employee and Customer. I created two new files in the models folder: customer.rb and employee.rb. Here is what employee.rb looks like: class Employee < User # model specific code here end you can do Employee.find(1) and it will return an Employee object from the users table. On Mar 5, 3:24 pm, Jonathon <t3hpwn...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hey guys, > > I''m not that great with Rails yet, but I''m trying to get there. > > I''m developing a site where users will be able to upload pictures and > videos (eventually). I want to set the pictures and the videos up as > separate models, or at least so they will act like it. Comments, tags, > and ratings will go onto the pictures and videos. I''ve decided the > easiest way to do it, while keeping everything nice and DRY, is to > create a "content_item" model, through which the pictures and videos > will act. > > I''m trying to find a way to basically do the following: > Picture.find(1) will go find the item with id = 1 in the content_items > model. If the content_type at id 1 isn''t a picture, it will return an > error. > Video.find_by_username("Foobar") would return all videos for the user > Foobar by using content_type "Video" > > I don''t think polymorphic associations applies to this, since the > content doesn''t has_one video, the content IS the video, if that makes > any sense. > > As of right now, I have a blank model. I know the answer is something > simple, but I just can''t seem to find it. > > Thanks, > > Jonathon--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
just wanted to add when you do something like Video.find(1) and item 1 is a picture it will raise ActiveRecord::RecordNotFound you can just run some code in your controller to handle errors from there. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---