> Here's an untested version that might be improved upon but nonetheless > might give you some ideas: > > %w{ inducer inhibitor substrate }.each do |thing| > condition = > instance_variable_set("@#{thing}s", > Interaction.find(:all, > :conditions => "involvement_type = #{thing.upcase}, > :include => "drug").map {|i| i.drug.name}.uniq > end > > I've taken the liberty of changing Drug.find(interaction.drug_id).name > to i.drug, on the theory that if interactions have a drug_id field, > then they belong_to :drug, and therefore should have a drug method. change upcase to capitalize (and pull out the line condition= ) and I think this'll be a massive improvement. I do like the solutions that made it all one SQL call, though. this one builds on what you've done here and merges it as one SQL query: @interactions = Interaction.find(:all, :conditions => ["involvement_type in (?)", %w(Inducer Inhibitor Substrate)], :include => :drugs).group_by(&:involvement_type) @interactions.keys.each do |key| instance_variable_set( "@#{key}", interactions[key].map { |i|i.drug.name}.to_set ) end just for perspective, though, these three involvement types are the only possible involvement types. so merging both solutions you could probably do something like: Interaction.find_all.group_by(involvement_type).keys.each do |key| instance_variable_set( "@#{key.downcase.pluralize}", interactions[key].map { |i|i.drug.name}.to_set ) end that is in fact staggeringly more elegant than the monstrosity I started this off with. the only hesitation I have there is that it could in fact be too compact for subsequent programmers to comfortably maintain. (the other thing is I'm on a client that doesn't use a fixed-width font so I can't be sure of the indentation, although that's hardly mission-critical.) still, 15 lines of code down to 4 is pretty good! if you look at the time on the original message you'll realize I am in fact in need of sleep, but I should be able to pop this into the app pretty soon to see what happens. -- Giles Bowkett http://www.gilesgoatboy.org