with_scope 引发“MySQL::Error Unkown column” RoR 2.3.11 中的错误

发布于 2024-11-26 05:48:50 字数 1832 浏览 0 评论 0原文

在旧代码中给出此模型,使用 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"

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

Given this model in legacy code, with 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

Whenever it reaches the Assignment.with_scope statement, I get the following exception:

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

My schema looks like this:

  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"

I have almost the same code across multiple models, but everything works fine there. Could you give me a hint, on how to tackle this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半枫 2024-12-03 05:48:50

执行以下操作后,它可以工作:

 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 开发人员进行审核。

After doing the following it works:

 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

This answer needs review from a more experienced Ruby on Rails developer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文