Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.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 Rails 4和引用'中的父id;有很多';SQL_Ruby On Rails_Associations_Has Many - Fatal编程技术网

Ruby on rails Rails 4和引用'中的父id;有很多';SQL

Ruby on rails Rails 4和引用'中的父id;有很多';SQL,ruby-on-rails,associations,has-many,Ruby On Rails,Associations,Has Many,TL;DR:如何在has\ MU many SQL子句中使用相应父对象的ID来查找子对象 长版本: 我有以下示例代码: class Person < AR::Base has_many :purchases, -> { "SELECT * from purchases WHERE purchase.seller_id = #{id} OR purchase.buyer_id = #{id}" } 我想在一个关联中查找与Person对象

TL;DR:如何在has\ MU many SQL子句中使用相应父对象的ID来查找子对象

长版本: 我有以下示例代码:

class Person < AR::Base
  has_many :purchases, -> { 
    "SELECT * from purchases
      WHERE purchase.seller_id = #{id}
         OR purchase.buyer_id = #{id}"
  }
我想在一个关联中查找与Person对象关联的所有购买,无论此人是销售对象的人还是购买对象的人

更新:我更正了SQL,它是由内而外的。很抱歉另外:关联只需要是只读的:我永远不会使用此关联创建记录,因此使用
id
两次应该可以。但是我确实希望能够在它上面链接其他作用域,例如
@person.purchases.paid.last_5
,因此在一个方法中创建两个关联的总和不起作用(至少在Rails 3中不起作用),因为它不返回AM::Relation,而是返回一个简单数组

在Rails 4.2中使用上述定义时,我得到

> Person.first.purchases
undefined method `id' for #<Person::ActiveRecord_Relation:0x...>
>Person.first.purchases
未定义的方法“id”#
错误是显而易见的,但是我该如何解决这个问题呢

由于这只是一个更复杂的SQL代码的示例,用于表示多个关系(例如,多个连接与子选择以提高性能),因此问题是:


如何在has\u many SQL子句中使用父对象的ID?

我认为您的代码根本无法工作。您正在定义与两个外键的关联。。。这意味着,如果您想从当前的
购买
中创建一个新的
人员
,将使用什么外键,
卖方id
买方id
?这根本没有道理

在任何情况下,您得到的错误都是明确的:您正在调用一个变量
id
,该变量未在SQL代码的块上下文中初始化

我从你的问题中了解到,解决这个问题的更好方法是以以下方式使用关联,然后定义一种方法,为你提供产品拥有的所有人,包括买家和卖家。大概是这样的:

class Purchase < ActiveRecord::Base
  belongs_to :buyer, class_name: 'Person'
  belongs_to :seller, class_name: 'Person'

  def persons
    ids = (buyer_ids + seller_ids).uniq
    Person.where(ids: id)
  end 
end

class Person < ActiveRecord::Base
  has_many :sold_purchases, class_name: 'Purchase', foreign_key: 'buyer_id'
  has_many :buyed_purchases, class_name: 'Purchase', foreign_key: 'seller_id'
end
class-Purchase
在我的方法中,
buyer\u id
seller\u id
是购买者的属性,而不是个人的属性


我可能没有正确理解,在这种情况下,请澄清。

对不起,我的代码弄乱了,我在上面更正了它。四年前,当项目开始使用Rails2.3时,我尝试使用另一种方法。但是,由于我希望能够在@purchase.persons上链接其他作用域(请参见编辑),这实际上不起作用(而且从未起过作用)。我需要一个“真实”的关联对象。如果您需要范围,您也可以使用我的编辑来完成。真的,谢谢,这是可行的,但它需要为
人员
进行三次SQL往返,其中一次可能是相当长的SQL字符串,具体取决于有多少
\u ID
。我想知道这是否可以在一个SQL命令中完成…:)我决定只使用一种方法(比如上面的
persons
,但没有范围-毕竟我不需要关联提供给您的大部分功能)。我想,对于我想要实现的目标,联想根本不是正确的工具。谢谢你让我停下来再想想……)非常乐意帮忙:)
class Purchase < ActiveRecord::Base
  belongs_to :buyer, class_name: 'Person'
  belongs_to :seller, class_name: 'Person'

  def persons
    ids = (buyer_ids + seller_ids).uniq
    Person.where(ids: id)
  end 
end

class Person < ActiveRecord::Base
  has_many :sold_purchases, class_name: 'Purchase', foreign_key: 'buyer_id'
  has_many :buyed_purchases, class_name: 'Purchase', foreign_key: 'seller_id'
end