Ruby on rails 过帐保存在错误的数据库表中

Ruby on rails 过帐保存在错误的数据库表中,ruby-on-rails,ruby,postgresql,Ruby On Rails,Ruby,Postgresql,我有一个非常复杂的RubyonRails网站。我没有编写代码,也不太了解RubyonRails 站点部署在双服务器上 Postgresql 9.0 redis 2.6.13 ruby 1.9.3 RubyonRails3 我添加了一个新表,并且几乎复制了另一个表(控制器、模型、视图),在我将一条新记录保存到数据库之前,一切似乎都正常工作 目前,我正在使用php脚本从csv文件手动将所有记录插入postgres 除了从网站上保存帖子外,一切都很好。。我甚至可以在ruby站点上更新通过php脚本手动

我有一个非常复杂的RubyonRails网站。我没有编写代码,也不太了解RubyonRails

站点部署在双服务器上

Postgresql 9.0 redis 2.6.13 ruby 1.9.3 RubyonRails3

我添加了一个新表,并且几乎复制了另一个表(控制器、模型、视图),在我将一条新记录保存到数据库之前,一切似乎都正常工作

目前,我正在使用php脚本从csv文件手动将所有记录插入postgres

除了从网站上保存帖子外,一切都很好。。我甚至可以在ruby站点上更新通过php脚本手动输入的帖子


如果我遗漏了任何可能需要显示的内容,请让我知道我将发布任何必要的内容


当我尝试在我的站点上发布新的热负载时,它会保存到负载表中

正如您所看到的,表单发布正确:(我将一步一步地发布所有内容)

步骤1:表格

EZPOST(HTML)

热加载控制器:(在这个阶段,它会拉右视图页面。)

工人:用户(u)海报

class UserPoster
  @queue = :user_posting

  def self.perform(_post)
    post_type=Load
    post_type=Hotload if (_post['hotload'])
    post_type=Truck if (_post['available'])
    result=post_type.send("post",_post)
    Notification.check(result.id, post_type)
  end

end
热负荷控制器(post)

全热负荷模型

class Hotload < ActiveRecord::Base
  validates :user_id, :uniqueness => {:scope => [:origin, :dest, :equipment_id, :length, :ltl, :pickup, :delivery, :rate, :weight]}
  attr_accessible :comments, :covered, :delivery, :dest, :equipment_id, :length, :ltl, :origin, :pickup, :rate, :user_id, :weight
  belongs_to :user
  has_one :equipment
  set_rgeo_factory_for_column(:origin, RGeo::Geographic.spherical_factory(:srid => 4326))
  set_rgeo_factory_for_column(:dest, RGeo::Geographic.spherical_factory(:srid => 4326))
  before_save :add_city_and_state

  def self.post(post)
    origin_state = post['origin'][-2,2]
    origin_city = post['origin'].gsub(%r(, [a-zA-Z]+), '').strip 
    dest_state = post['dest'][-2,2]
    dest_city = post['dest'].gsub(%r(, [a-zA-Z]+), '').strip 
    origin = Location.first(:conditions => {:city => origin_city.downcase, :state => origin_state.downcase}) 
    dest = Location.first(:conditions => {:city => dest_city.downcase, :state => dest_state.downcase})
    if origin.nil? or dest.nil?
     origin = Location.where("levenshtein(city, '#{origin_city.downcase}') <= 4 AND state = '#{origin_state.downcase}'").first
     dest = Location.where("levenshtein(city, '#{dest_city.downcase}') <= 4 AND state = '#{dest_state.downcase}'").first
    end
    post['origin'] = "POINT (#{origin.coords.x} #{origin.coords.y})"
    post['dest'] = "POINT (#{dest.coords.x} #{dest.coords.y})"
    l = Hotload.new(post)
    k = post.keys
    k.each do |key|
      if post[key] = ''
        post.delete(key) 
      end
    end
    l.save!
    l
  end

  def self.suggested_hotloads(user_id)
    @user = User.find(user_id)
    now = Time.now.strftime("%F")
    user_notifier = Notification.where(user_id: @user.id).first
    user_zip = @user.company.zip
    user_location = Location.where(zip: user_zip).first
    @suggested_hotloads = []
    begin
      notifier_hotloads = Hotload.where("origin = '#{user_notifier.origin}' OR dest = '#{user_notifier.dest}' AND pickup > '#{now}'").limit(25).order('id DESC')
      notifier_hotloads.each {|x| @suggested_hotloads.push x } if notifier_hotloads
    rescue Exception => e
      unless user_notifier.blank?
        if !user_notifier.origin.blank?
          notifier_hotloads = Hotload.where("origin = '#{user_notifier.origin}' AND pickup > '#{now}'").limit(25).order('id DESC')
        elsif !user_notifier.dest.blank?
          notifier_hotloads = Hotload.where("origin = '#{user_notifier.dest}' AND pickup > '#{now}'").limit(25).order('id DESC')
        end
        notifier_hotloads.each {|x| @suggested_hotloads.push x } if notifier_hotloads
      end
    end
    begin
      location_hotloads = Hotload.where("origin = '#{user_location.coords}' OR dest = '#{user_location.coords}' AND pickup > '#{now}'").limit(25).order('id DESC')
      location_hotloads.each {|x| @suggested_hotloads.push x } if location_hotloads
    rescue Exception => e
      puts e unless Rails.env.test?
    end
    @recents = Hotload.where("rate > 0 AND pickup > '#{now}'").order('id DESC')
    @recents.each do |recent|
      if @suggested_hotloads.count < 50
        @suggested_hotloads.push recent
      end
    end
    return @suggested_hotloads
  end

  def self.return_hotloads(hotload)
    if hotload.delivery.blank? then hotload.delivery = Time.now end
    Hotload.where(
      "ST_Distance(origin, ST_GeomFromText('#{hotload.dest}')) <= 100*1609.334
      AND ST_Distance(dest, ST_GeomFromText('#{hotload.origin}')) <= 100*1609.334
      AND pickup BETWEEN '#{hotload.delivery}' AND '#{hotload.delivery+3.days}'
            AND equipment_id = '#{hotload.equipment_id}'"
    ).limit(5)
  end

  private
    def add_city_and_state
      origin = Location.city_state(self.origin)
      dest = Location.city_state(self.dest)
      if origin == "ANYWHERE"
        self.origin_city, self.origin_state = nil, nil
      else
        self.origin_city = origin[:city]
        self.origin_state = origin[:state]
      end
      if dest == "ANYWHERE"
        self.dest_city, self.dest_state = nil, nil
      else
        self.dest_city = dest[:city]
        self.dest_state = dest[:state]
      end
    end
end

查看日志后:

2014-05-12 10:21:25 INFO --   Parameters: {"load"=>{"comments"=>"Posted From LoadMax EZ-Post Form", "dest"=>"st louis, mo", "equipment_id"=>"8", "hotload"=>"true", "origin"=>"springfield, mo", "pickup"=>"2014-05-12", "user_id"=>"10181"}, "id"=>"1"}
可能是您的
重定向到
的代码正在以
参数[post]
的形式传递
:load
,而此时它应该是
:hotload
。如果您在UserController中添加第二个可选的
重定向\u到
代码,是否有效:

  def ezpost
    type = params[:post].delete(:type)
    Resque.enqueue(UserPoster, params[:post])
    redirect_to :controller => "#{type}s", :action => 'show', :id => 1, :load => params[:post]
  end
    def ezpost
        type = params[:post].delete(:type)
        Resque.enqueue(UserPoster, params[:post])

        if params[:post][:type] == "load"
           redirect_to :controller => "#{type}s", :action => 'show', :id => 1, :load => params[:post]
        elsif params[:post][:type] == "hotload"
           redirect_to :controller => "#{type}s", :action => 'show', :id => 1, :hotload => params[:post]
        else #truck
           redirect_to :controller => "#{type}s", :action => 'show', :id => 1, :truck => params[:post]
        end
    end

我还没有测试过代码,但希望它能帮助你

你的问题似乎出在
perform
方法上,因为这是做出决定的地方,也是我能看到的唯一一个建议加载或Hoatload的分支路径的代码。它可以像在
\u post
参数中使用符号一样简单。试试这个

class UserPoster
  @queue = :user_posting

  def self.perform(_post)
    post_type=Load
    post_type=Hotload if (_post[:hotload])
    post_type=Truck if (_post[:available])
    result=post_type.send("post",_post)
    Notification.check(result.id, post_type)
  end

end
在我看来,它在开始时将post_type设置为Load,然后
post_type
没有返回True,因此它在结束时执行
Load.post\u post


如果不是这样,你应该努力找出为什么post['hotload']没有像预期的那样返回true。您可以使用Rails.logger.info _post['hotload']查看其值。您可能希望执行
Rails.logger.info“预期值为#{u post['hotload']}”
这样,如果根本不起作用,您就不会得到空行。

您到底被困在哪里??我无法理解你上面解释的内容。。。你能更具体一点吗?我已经添加了新的热负荷表。出于某种原因,我不明白为什么当我发布一个新的热加载时,它会将其保存到加载表中。我想它可能在工人中的某个地方,因为我安装了redis。除了保存到错误的表之外,其他一切似乎都正常。我无法让它记录任何内容。请参阅帖子底部的更新脚本以进行记录。我将其作为加载传递,因为我使用该表单进行3种不同类型的发布。装载,热装载,卡车。一旦它到达正确的控制器ex hotload。我有@hotload=hotload.new(参数[:load])。。。但它应该在从用户控制器到达之前保存。。。enqueue(UserPoster,params[:post])感谢您的澄清,您的Rails代码确实非常先进。我刚刚看了一眼Resque在铁路上的表现,这对我来说是全新的。显然,可能会有一些延误,但我不确定这是否对本案有任何影响。你知道问题是什么吗?嗯,我只是在def ezpost上加了一辆卡车以防万一。那个部分工作得很好。工作不正常的部分是Resque.enqueue(UserPoster,params[:post]),我无法在上面的记录器上获得任何值。。。我尝试了.info和.debug以及.errorBTW发布一辆卡车,它与上面的代码post_type=Truck if(_post['available'])一起工作,所以hotload应该差不多,如果你只做
_post['hotload'].foo,看看它崩溃时会报告什么?你会得到一个命名者。如果它来自Nil,那么您知道您实际上并没有传递您期望的值。您也可以尝试
放置_post['hotload']
,它将输出到您的服务器控制台。您还可以尝试
@logger.error“预期输出:{_post['hotload']}”
因此,您知道日志记录失败是由于日志记录错误造成的,还是因为日志没有告诉您什么而没有向日志发送任何内容。lol我是Ruby on Rails的新手,使用该脚本,我无法找到日志文件我知道的任何日志文件
class Hotload < ActiveRecord::Base
  validates :user_id, :uniqueness => {:scope => [:origin, :dest, :equipment_id, :length, :ltl, :pickup, :delivery, :rate, :weight]}
  attr_accessible :comments, :covered, :delivery, :dest, :equipment_id, :length, :ltl, :origin, :pickup, :rate, :user_id, :weight
  belongs_to :user
  has_one :equipment
  set_rgeo_factory_for_column(:origin, RGeo::Geographic.spherical_factory(:srid => 4326))
  set_rgeo_factory_for_column(:dest, RGeo::Geographic.spherical_factory(:srid => 4326))
  before_save :add_city_and_state

  def self.post(post)
    origin_state = post['origin'][-2,2]
    origin_city = post['origin'].gsub(%r(, [a-zA-Z]+), '').strip 
    dest_state = post['dest'][-2,2]
    dest_city = post['dest'].gsub(%r(, [a-zA-Z]+), '').strip 
    origin = Location.first(:conditions => {:city => origin_city.downcase, :state => origin_state.downcase}) 
    dest = Location.first(:conditions => {:city => dest_city.downcase, :state => dest_state.downcase})
    if origin.nil? or dest.nil?
     origin = Location.where("levenshtein(city, '#{origin_city.downcase}') <= 4 AND state = '#{origin_state.downcase}'").first
     dest = Location.where("levenshtein(city, '#{dest_city.downcase}') <= 4 AND state = '#{dest_state.downcase}'").first
    end
    post['origin'] = "POINT (#{origin.coords.x} #{origin.coords.y})"
    post['dest'] = "POINT (#{dest.coords.x} #{dest.coords.y})"
    l = Hotload.new(post)
    k = post.keys
    k.each do |key|
      if post[key] = ''
        post.delete(key) 
      end
    end
    l.save!
    l
  end

  def self.suggested_hotloads(user_id)
    @user = User.find(user_id)
    now = Time.now.strftime("%F")
    user_notifier = Notification.where(user_id: @user.id).first
    user_zip = @user.company.zip
    user_location = Location.where(zip: user_zip).first
    @suggested_hotloads = []
    begin
      notifier_hotloads = Hotload.where("origin = '#{user_notifier.origin}' OR dest = '#{user_notifier.dest}' AND pickup > '#{now}'").limit(25).order('id DESC')
      notifier_hotloads.each {|x| @suggested_hotloads.push x } if notifier_hotloads
    rescue Exception => e
      unless user_notifier.blank?
        if !user_notifier.origin.blank?
          notifier_hotloads = Hotload.where("origin = '#{user_notifier.origin}' AND pickup > '#{now}'").limit(25).order('id DESC')
        elsif !user_notifier.dest.blank?
          notifier_hotloads = Hotload.where("origin = '#{user_notifier.dest}' AND pickup > '#{now}'").limit(25).order('id DESC')
        end
        notifier_hotloads.each {|x| @suggested_hotloads.push x } if notifier_hotloads
      end
    end
    begin
      location_hotloads = Hotload.where("origin = '#{user_location.coords}' OR dest = '#{user_location.coords}' AND pickup > '#{now}'").limit(25).order('id DESC')
      location_hotloads.each {|x| @suggested_hotloads.push x } if location_hotloads
    rescue Exception => e
      puts e unless Rails.env.test?
    end
    @recents = Hotload.where("rate > 0 AND pickup > '#{now}'").order('id DESC')
    @recents.each do |recent|
      if @suggested_hotloads.count < 50
        @suggested_hotloads.push recent
      end
    end
    return @suggested_hotloads
  end

  def self.return_hotloads(hotload)
    if hotload.delivery.blank? then hotload.delivery = Time.now end
    Hotload.where(
      "ST_Distance(origin, ST_GeomFromText('#{hotload.dest}')) <= 100*1609.334
      AND ST_Distance(dest, ST_GeomFromText('#{hotload.origin}')) <= 100*1609.334
      AND pickup BETWEEN '#{hotload.delivery}' AND '#{hotload.delivery+3.days}'
            AND equipment_id = '#{hotload.equipment_id}'"
    ).limit(5)
  end

  private
    def add_city_and_state
      origin = Location.city_state(self.origin)
      dest = Location.city_state(self.dest)
      if origin == "ANYWHERE"
        self.origin_city, self.origin_state = nil, nil
      else
        self.origin_city = origin[:city]
        self.origin_state = origin[:state]
      end
      if dest == "ANYWHERE"
        self.dest_city, self.dest_state = nil, nil
      else
        self.dest_city = dest[:city]
        self.dest_state = dest[:state]
      end
    end
end
class UserPoster
  @queue = :user_posting

  def self.perform(_post)
    post_type=Load
    post_type=Hotload if (_post['hotload'])
    @log.error _post['hotload']
    post_type=Truck if (_post['available'])
    @log.error _post['avaliable']
    result=post_type.send("post",_post)
    Notification.check(result.id, post_type)
  end
  @log = Logger.new("#{Rails.root}/log/userposter.log")
  @log.datetime_format = "%F %T"
end
2014-05-12 10:21:25 INFO --   Parameters: {"load"=>{"comments"=>"Posted From LoadMax EZ-Post Form", "dest"=>"st louis, mo", "equipment_id"=>"8", "hotload"=>"true", "origin"=>"springfield, mo", "pickup"=>"2014-05-12", "user_id"=>"10181"}, "id"=>"1"}
    def ezpost
        type = params[:post].delete(:type)
        Resque.enqueue(UserPoster, params[:post])

        if params[:post][:type] == "load"
           redirect_to :controller => "#{type}s", :action => 'show', :id => 1, :load => params[:post]
        elsif params[:post][:type] == "hotload"
           redirect_to :controller => "#{type}s", :action => 'show', :id => 1, :hotload => params[:post]
        else #truck
           redirect_to :controller => "#{type}s", :action => 'show', :id => 1, :truck => params[:post]
        end
    end
class UserPoster
  @queue = :user_posting

  def self.perform(_post)
    post_type=Load
    post_type=Hotload if (_post[:hotload])
    post_type=Truck if (_post[:available])
    result=post_type.send("post",_post)
    Notification.check(result.id, post_type)
  end

end