Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 RubyonRails3重构_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 3 RubyonRails3重构

Ruby on rails 3 RubyonRails3重构,ruby-on-rails-3,Ruby On Rails 3,我已经问了那个问题,但没有人回答,所以我会尝试清理一下,然后再问 我有一个rails 3应用程序,它保存不同公司的信息,每家公司都有与之相关的信息、期限、地区、价格等。我想做的搜索是根据期限、地区等查找每家公司的价格。从表单中选择的所有内容。当我在视图中执行操作时,一切正常,如下所示: <% for company in Company.all @territory_group = Territory.territory_group_id(params[:territory], comp

我已经问了那个问题,但没有人回答,所以我会尝试清理一下,然后再问

我有一个rails 3应用程序,它保存不同公司的信息,每家公司都有与之相关的信息、期限、地区、价格等。我想做的搜索是根据期限、地区等查找每家公司的价格。从表单中选择的所有内容。当我在视图中执行操作时,一切正常,如下所示:

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

结果是:

    <% for price in @prices %>
     <%= price.company.name %>
     <%= price.program.name %>
     <%= price.price %>
    <% end %>

#END company loop
<% end %>

#终端公司环路
模型-模型之间的关联是这样建立的,因为很少有其他模型与它们关联,因此无法进行协同

#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
#地域模式-每家公司都有不同的地域分组组织。
#c(company_id)只是通过id查找公司
_和_是否属于多个:区域组
def自身区域组(区域、公司id)
z=查找(区域)
z、 territory_groups.c(公司识别号)。第一
结束
#TerritoryGroup模型
你和你属于很多领域吗
是否有很多价格
属于:公司
#期限模型保留时间段-期限从、期限到和期限作为固定日期
你和你属于很多公司吗
_和_属于许多:术语类型
#c&t是整数/c-公司,t-术语
范围:查找_-term,lambda{c,t | where(“company|u-id=?和term_-from=?或term=?和company_-id=?”,c,t,t,t,c)}
#术语类型模型–这里的关系是,根据公司的不同,术语可以有不同的术语类型
有很多术语吗
#价格模型属于几个不同的模型,接受不同的参数
#范围:c_价格–查找每个公司、条款和一些其他参数的价格
属于:公司
属于:术语
_和_是否属于多个:区域组
我的问题是如何重构所有这些,因此视图中唯一剩下的就是显示搜索结果,以便稍后我可以将结果导出为xml和json


提前谢谢

对于初学者,以下代码:

@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])
可以抽象到您的控制器中,以进行适当的操作(我将假定为索引)。当您的索引操作被调用时,
@prices
(以及
@territory\u group
@term\u type\u id
@term
)将在相应的视图中可用,并且您的
for
循环应该仍然有效。对于控制器中声明的所有实例变量,这应该是真的;也就是说,它们应该在声明它们的操作的相应视图中可用

示例
公司\u 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

每个公司的价格都不一样,所以运行第一个循环,我得到公司,通过与期限、地区等的活动记录关联,我得到我想要的价格,然后通过新的循环,我最终得到每个公司需要的价格。我在终端中看到了查询,一切似乎都很好,但当转到视图@prices为空时。我实际上尝试了这种方式,并在prices中得到了fixnum错误,没有效果:-)我是说,在我发布之前,我尝试了完全相同的方式并得到了一个错误,但现在工作很好。非常感谢。如果我想在price\u set循环中为price做一些额外的工作,并在控制器中抽象循环,该怎么办。例如,循环在控制器中,如果公司有折扣或增加,我会根据公司特有的一些变量更改价格。那么,我如何将结果发布到视图中呢?首先,我承认:我正在学习Rails!话虽如此,你的问题很可能有一个简单直接的答案,但我不知道。似乎很难将price_set循环抽象到控制器。价格在循环中有点“锁定”,所以我不确定目前如何增加折扣等。不过我会继续考虑的!
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 %>