I have asked that question but there is no response so i'll try to clean it up and ask again.
I have an rails 3 application that holds information about different companies and each company has information associate with it, term, territory, price and etc. The search I would like to do is to find the price for each company based on the length of term, territory and so on. All that is selected from a form. All is working fine when I do it in the view, like so:
<%
for company in Company.all
@territory_group = Territory.territory_group_id(params[:territory], company.id)
@term_type_id = TermType.find(params[:term_type])
@term = @term_type_id.terms.find_term(company.id, params[:term_id])
@prices = @territory_group.prices.c_prices(company.id, @term.id, params[:else])
%>
The result is:
<% for price in @prices %>
<%= price.company.name %>
<%= price.program.name %>
<%= price.price %>
<% end %>
#END company loop
<% end %>
Models - the associations between the models are made like this because there are few other models associat with them and cobining is not possible
#Territory model - Each company has different territories organized in groups.
#c(company_id) just finds the company by id
has_and_belongs_to_many :territory_groups
def self.territory_group(territory, company_id)
z = find(territory)
z.territory_groups.c(company_id).first
end
#TerritoryGroup model
has_and_belongs_to_many :territories
has_and_belongs_to_many :prices
belongs_to :company
#Term model hold periods of time - term_from, term_to and term as fixed date
has_and_belongs_to_many :companies
has_and_belongs_to_many :term_types
# c & t are integers / c - company, t - term
scope :find_term, lambda {|c,t| where("company_id = ? AND term_from <= ? AND term_to >= ? OR term = ? AND company_id = ? ", c,t,t,t,c)}
#TermType model – the ralation here is that term can have different term types depending on the company
has_and_belongs_to_many :terms
#Price model belongs to few different models and accepts different params
#scope :c_price – finds the price for each company, term and some other parameters
belongs_to :company
belongs_to :term
has_and_belongs_to_many :territory_groups
My question is how to refactor all this, so the only thing left in the view is showing the result of the search, so later on I can export the results in xml and json?
Thx in advance!