【问题标题】:with_scope raising "MySQL::Error Unkown column" error in RoR 2.3.11with_scope 在 RoR 2.3.11 中引发“MySQL::Error Unknown column”错误
【发布时间】:2011-07-25 18:58:24
【问题描述】:

在遗留代码中使用此模型,使用 RoR 2.3.11:

class Assignment < ActiveRecord::Base
  belongs_to :source_person 
  belongs_to :destination_person
  belongs_to :laptop

  def self.setScope(places_ids)
    find_include = [:laptop => {:owner => {:performs => {:place => :ancestor_dependencies}}}]
    find_conditions = ["place_dependencies.ancestor_id = (?)", places_ids]

    scope = { :find => { :conditions => find_conditions, :include => find_include } }
    Assignment.with_scope(scope) do
      yield
    end
  end
end

每当它到达Assignment.with_scope 语句时,我都会收到以下异常:

 Assignment Delete all (0.0ms)   Mysql::Error: Unknown column 'place_dependencies.ancestor_id' in 'where clause': DELETE FROM `assignments` WHERE (`id` IN (17)) AND (place_dependencies.ancestor_id = (1)) 

我的架构如下所示:

  create_table "assignments", :force => true do |t|
    t.date    "created_at"
    t.date    "date_assigned"
    t.time    "time_assigned"
    t.integer "source_person_id"
    t.integer "destination_person_id"
    t.integer "laptop_id"
    t.text    "comment"
  end

  add_index "assignments", ["destination_person_id"], :name => "assignments_destination_person_id_fk"
  add_index "assignments", ["laptop_id"], :name => "assignments_laptop_id_fk"
  add_index "assignments", ["source_person_id"], :name => "assignments_source_person_id_fk

  create_table "place_dependencies", :force => true do |t|
    t.integer "descendant_id"
    t.integer "ancestor_id"
  end

  add_index "place_dependencies", ["ancestor_id"], :name => "place_dependencies_ancestor_id_fk"
  add_index "place_dependencies", ["descendant_id"], :name => "place_dependencies_descendant_id_fk"

我在多个模型中拥有几乎相同的代码,但在那里一切正常。你能给我一个提示,关于如何解决这个问题吗?

【问题讨论】:

    标签: ruby-on-rails ruby activerecord ruby-on-rails-2


    【解决方案1】:

    执行以下操作后:

     def self.setScope(places_ids)
        find_include = {
          :laptop => {
            :owner => {
              :performs => {
                :place => :ancestor_dependencies}
            }
          }
        }
        find_conditions = {"place_dependencies.ancestor_id" => places_ids}
    
        Assignment.all :joins => find_include, :conditions => find_conditions
        yield
      end
    

    此答案需要由更有经验的 Ruby on Rails 开发人员审核。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-06
      • 2017-09-02
      • 2017-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-03
      相关资源
      最近更新 更多