Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6369195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:46:21+00:00 2026-05-25T00:46:21+00:00

I have asked that question but there is no response so i’ll try to

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:46:21+00:00Added an answer on May 25, 2026 at 12:46 am

    For starters, the following code:

    @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])
    

    could be abstracted into your controller for the appropriate action (which I will assume to be Index). When your Index action is called, then, @prices (as well as @territory_group, @term_type_id, and @term) will be available in the corresponding view, and your for loop should still work. This should be true for all instance variables declared in your controllers; that is, they should be available in the corresponding views for the actions in which they’re declared.

    Example companies_controller.rb:

    class CompaniesController << ApplicationController
    ...
      def index
        @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])
    
        respond_to do |format|
          format.html
          format.xml { render :xml => @prices }
        end
      end
    ...
    end
    

    In general, you want to keep your logic in a controller, as opposed to a view.

    Is this what you were looking for? Or am I missing something else?

    EDIT
    Could you capture the results from your company loop in an array, and then run through them using the each method? Like so:

    class CompaniesController << ApplicationController
    ...
      def index
        @prices = []
    
        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])
        end
    
        render_to do |format|
          format.html
          format.xml { render :xml => @prices }
        end
      end
    ...
    end
    
    
    
    [VIEW:]
    
    <% @prices.each do |price_set| %>
      <% for price in price_set %>
        <%= price.company.name %>
        <%= price.program.name %>
        <%= price.price %>
      <% end %>
    <% end %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have asked this question before but no answer was there. so asking again.
I realize that this question has been asked 100times but none that I have
Hi I know this question have been asked before but the answers there isn't
I know that variations on this question have been asked, but I've tried all
I am sure that this kind of questions must have been asked before, but
This question is tied in with this other question that I asked I have
Question: I have a question that is apparently not answered by this already-asked Bash
I'm re-casting a question I asked earlier now that I have an idea of
I have been asked to build a web application that will be used to
I have been asked to write an application that is going to act as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.