So I'm trying to come up with a smart/the best rails way to do this, and would appreciate discussing with SO users the best method.
Essentially I have two models, let's call the first a Photo and the second an Attribute. A photo has_many attributes (as examples, let's give saturation, color, warmth etc..). The Attribute model carries and name and id. The Rating model carries a single integer field. The Photo and Attribute models are related through a PhotoRelationship model, through a polymorphic association. The reason for this is that a photo has_many other relationships that I store in this table (rather than having a multitude of relationship tables). Assume I have to use these models, as they are already in place, but I can alter them or add additional models. Here is how the models' relationships work:
in Photo.rb:
has_many :photo_relationships
has_many :attributes, :through => :photo_relationships, :source => :related_attribute, :source_type => "Attribute"
in PhotoRelationship.rb
belongs_to :photo #photo_id
belongs_to :related_attribute, :polymorphic => true
in Attribute.rb
has_many :photo_relationships, :as => :related_attribute, :dependent => :destroy
has_many :photos, :through => :photo_relationships
Let's say I also want to rate the Photo model's Attributes. I introduce a third model Rating, because each photo-attribute pairing will have a certain rating then associated with it.
I want to figure out how to run the relationship between a photo-attribute pairing and its rating. For example, let's say we have a photo with a certain number of attributes and we want to find the rating of, say, the first attribute of that photo. Then:
photo = Photo.first
photo.attributes.first.rating
How would I set it up in the database? I suppose I could add a rating_id field to the PhotoRelationship model, which already has unique entries for each photo-attribute pair.
Could this work, could I somehow relate the attribute to its rating?? Something tells me something is missing. I am getting the feeling that I would need to have a separate AttributeRelationship table that is non-polymorphic and even in this case, I don't see how the relationships work.
Any info anyone can provide, thoughts, or alternate suggestions on how to do this would be greatly appreciated! Thanks!