views:

101

answers:

2

I have this:

class User < ActiveRecord::Base
  has_many :serials
  has_many :sites, :through => :series
end

class Serial < ActiveRecord::Base
  belongs_to :user
  belongs_to :site
  has_many :episodes
end

class Site < ActiveRecord::Base
  has_many :serials
  has_many :users, :through => :serials
end

class Episode < ActiveRecord::Base
  belongs_to :serial
end

I would like to do some operations on User.serials.episodes but I know this would mean all sorts of clever tricks. I could in theory just put all the episode data into serial (denormalize) and then group_by Site when needed.

If I have a lot of episodes that I need to query on would this be a bad idea?

thanks

A: 

It really depends on the scale you are needing out of this application. If the app isn't going to need to serve tons and tons of people then go for it. If you are getting a lot of benefit from the active record associations then go ahead and use them. As your application scales you may find yourself replacing specific instances of the association use with a more direct approach to handle your traffic load though.

imightbeinatree at Cloudspace
A: 

I wouldn't bother denormalizing.

If you need to look at counts, you can check out counter_cache on the relationship to save querying for that.

Do you have proper indexes on your foreign keys? If so, pulling the data from one extra join shouldn't be that big of a deal, but you might need to drop down to SQL to get all the results in one query without iterating over .serials:

User.serials.collect { |s| s.episodes }.uniq  # ack! this could be bad
wesgarrison