On Monday, August 26, 2013 11:38:02 AM UTC-7, Adam Gamble wrote:
>
> Hi Jeremy et al,
>
> Just had a chance to try out unionising those two comments tables,
> however, I notice that in creating the union the comment id's become
> redundant as e.g. {id: 1} exists for both the `photo_comment` and the
> `photoset_comment` tables. Is there a trick to creating mutually unique
> keys for tables in union?
>
> Not too far into it, my query is also insufficient..
>
> ```
> PhotoSetComment.where(:photoset_id => sid)
> .union(PhotoComment.where(:photo_id => pid))
> ```
>
> Whereas I actually need comments for each `Photo.where(:photoset_id =>
> sid)`. Should I chain unions to do this, or does Ruby have some nice syntax
> to enumerate each Photo into a union?
>
>
Are you sure you actually need to do a union to get what you want or
polymorphic associations to get what you want? I would think some simple
eager loading would suffice if you just want to print out all comments for
a photoset and each photo in the photoset:
Photo.one_to_many :photos, :eager=>:comments
PhotoSet.one_to_many :comments, :class=>:PhotoSetComment
Photo.many_to_one :photoset
Photo.one_to_many :comments, :class=>:PhotoComment
photoset = Photoset[1]
photoset.comments # comments on Photoset
photoset.photos.each do |photo|
photo.comments # comments on this Photo
end
This does a total of 4 queries:
* Get photoset with id 1
* Get comments for photoset with id 1
* Get photos for photoset with id 1
* Get comments for all photos in photoset with id 1
Personally, I'm not a fan of GUID/UUID columns unless they are necessary
for other reasons. Certainly in this case I think they are a bad idea.
Polymorphic associations are frowned upon as they violate referential
integrity and cause unnecessary complexity.
If I had to store both photo and photoset comments in the same table, I
would do the following:
create_table(:comments) do
primary_key :id
foreign_key :photoset_id, :photosets, :null=>false
foreign_key :photo_id, :photos
String :comment, :null=>false
end
For photoset comments, the photo_id would be NULL. For photo comments,
both photoset_id and photo_id would be filled in. This would make it easy
to get all comments in a photoset, including ones for specific pictures in
the set.
Thanks,
Jeremy
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/groups/opt_out.