Hi there. I'm very new to RoR, and I'm writing my first 'web app' and already I think I need a one-to-many polymorphic join. The reason I'm writing this is because it is causing me a real headache. After sitting at my pc for what seems like 4 four solid days, I'm not getting anywhere. Here's the scenario; A user profile photo will be displayed based on which profile he/she has selected. There are three different types. a) Monthly photo b) Daily photo c) Specific date in year. Each of these are all different tables because they hold slightly different types of data. So 'Monthly' would have at most 12 rows per user, whereas 'daily photos' and 'specific date' tables would have an unlimited amount of rows of data assigned to a user. Does anyone know how best to go about to achieve this? I've tried using the example of the polymorphic join in AWDR V2, but it seems to be a one 2 one relationship. I can't seem to get the One 2 many to work at all. I suppose my question is this; Can anyone help with One to Many polymorpphic joins before the last of my hair falls out!
on 2008-11-02 15:44
on 2008-11-03 13:21
That's no polymorphic relation at all. In a polymorphic relation would mean, that the table contains one and the same kind of data for a set of other tables. Say you have client and admin tables and want all of them to have an address, then address would be polymorphic, assigned either to a client or address. In your case the user is the central table and the other three relate to it by simply having an user_id field. user has_many monthly_photos user has_many daily_photos user has_many date_photos daily_photos belongs_to :users monthly_photos belongs_to :users date_photos belongs_to :users You could enhance that code by using a single photo table with a kind_of field, that's saying if it's daily, monthly or fixed date and using a use_at date field, interpreting it depending on kind_of. Or use "Single table inheritance" (explained in Rails API docs ActiveRecord::Base) But I wouldn't do that for such a small piece of functionality.