I’ve got an application with 2 related models. I want to search for
data based on conditions in both models. I want to return items from
the first model (Contract) grouped by conditions in the second
(Status)
At the moment, I’m basically doing it as shown below. It seems really
ugly. Any suggestions how to do it better? Thanks
Nick
class Contract < ActiveRecord::Base
belongs_to :project
belongs_to :status
def self.activity_find(team, director)
conditions = Array.new
conditions << “projects.team_id = #{service_team}” if
service_team != “0”
conditions << “projects.director_id = #{bid_director}” if
bid_director != “0”
ces = Array.new
for group in Status::GROUPS
conditions << “statuses.” + Status.group_sql(group)
status_group = find(:all, :conditions => conditions.join(" AND
"), :include => [:status])
conditions.delete( conditions.last )
ces << {:data => status_group, :name => group[:description]}
end
end
end
class Status < ActiveRecord::Base
has_many :contracts
GROUPS = [{:stage => 1, :description => “been entered as
prospect”},
{:stage => 2, :description => “proposal has been
submitted”},
{:stage => 3, :description => “been notified as won”},
{:stage => 4, :description => “been contracted”}]
def self.group_sql(group)
case group[:stage]
when 1 then ‘opportunity=1’
when 2 then ‘confirmed=1’
when 3 then ‘notified=1’
when 4 then ‘contracted=1’
end
end