5d7c8b8f2de06c07b2bf0848d20ce71f

For a object (e.g. user), I want to retrieve the related source items. The object is assigned to sources and their respective source items.

# Associations
#
# User: has_many :sources, :as => :sourceable, :dependent => :destroy
#
# Source:
#   has_many :source_items
#   belongs_to :source_type
#   belongs_to :sourceable, :polymorphic => true
#
# SourceItem: belongs_to :source

# source.rb
def self.find_source_for obj
    obj.sources.all(:include => [:source_items => {:source => :source_type}]).collect { |s| s.source_items }.flatten!    # Can I limit here also the retrieval of source item?
end

Refactorings

No refactoring yet !

55502f40dc8b7c769880b10874abc9d0

ggomeze.myopenid.com

March 20, 2010, March 20, 2010 16:02, permalink

No rating. Login to rate!

Not sure this is what you mean, but i see some redundant code in your approach

obj.sources.map(&:source_items).flatten!
1b6e26633e1ebea63c21f126c3da08e0

cna training

April 5, 2010, April 05, 2010 23:50, permalink

No rating. Login to rate!

Wow this is a great resource.. I’m enjoying it.. good article

Your refactoring





Format Copy from initial code

or Cancel