Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.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
ActiveRecord 4如何使用用户:订单和:包括_Activerecord_Ruby On Rails 4 - Fatal编程技术网

ActiveRecord 4如何使用用户:订单和:包括

ActiveRecord 4如何使用用户:订单和:包括,activerecord,ruby-on-rails-4,Activerecord,Ruby On Rails 4,我刚刚升级到Ruby 2.0,默认情况下会为Padrino应用程序加载ActiveRecord 4。这很好,我想利用Ruby 2.0和AR 4的改进。然而,我很难重写一些以前有效的关系 例如,这不再有效: has_many :dvd_credits, :order => 'position', :include => [:dvd_actor, :dvd_role] 它给出了一个不推荐使用的警告,但它显示的示例是无用的:有很多:spam\u注释,->{where spam:tru

我刚刚升级到Ruby 2.0,默认情况下会为Padrino应用程序加载ActiveRecord 4。这很好,我想利用Ruby 2.0和AR 4的改进。然而,我很难重写一些以前有效的关系

例如,这不再有效:

  has_many :dvd_credits, :order => 'position', :include => [:dvd_actor, :dvd_role]
它给出了一个不推荐使用的警告,但它显示的示例是无用的:
有很多:spam\u注释,->{where spam:true},class\u name:'Comment'

环顾四周,我发现省略了
:include
参数,这是可行的:

has_many :dvd_credits, -> { order :position }
如果我这样加上:

has_many :dvd_credits, -> { order :position, include: [:dvd_actor, :dvd_role] }
当我调用该关系时,会出现以下错误:

ArgumentError: Direction should be :asc or :desc
我仍在努力应对AR 4的变化,但非常感谢您在这方面的帮助

谢谢

PS 我确实在AR 4页面中找到了这一点:

没有必要对直接关联使用include,也就是说, 如果您的订单属于:customer,则该客户是 需要时自动加载

我认为这意味着我可以安全地忽略
include:
参数

但这种关系有点复杂,它是这样的:

class Dvd < ActiveRecord::Base
  has_many :dvd_credits, -> { order :position }
  has_many :dvd_actors, :through => :dvd_credits
  has_many :dvd_roles, :through => :dvd_credits

class DvdCredit < ActiveRecord::Base
  belongs_to :dvd_actor, :inverse_of => :dvd_credits
  belongs_to :dvd_role, :inverse_of => :dvd_credits
  belongs_to :dvd, :inverse_of => :dvd_credits

class DvdActor < ActiveRecord::Base
  has_many :dvd_credits, :inverse_of => :dvd_actors
  has_many :dvds, :through => :dvd_credits
类Dvd{顺序:位置} 有很多:dvd演员,:至=>:dvd演员 拥有多个:dvd角色,:至=>:dvd学分 类DvdCredit:dvd演员 属于:dvd角色,:反转=>:dvd\u学分 属于_to:dvd,:倒数_of=>:dvd\u学分 类DvdActor:dvd演员 有很多:dvd,:至=>:dvd
但是似乎满足了
所属的
条件。

是的,您可以忽略
:include
部分