rails scope没有关联

时间:2016-11-10 22:50:38

标签: ruby-on-rails ruby-on-rails-4 scope

我有一个名为" activos"的模型,我只需要显示与另一个名为" relactivo"的模型无关的记录。

我在模型中尝试过这个:范围:ts, - > {includes(:relactivo).where(relactivo:{activo:nil})}

这是我的模型" activos"

class Activo < ActiveRecord::Base
  self.primary_key = "IdActivos"

  scope :ts, -> { includes(:relactivo).where(relactivo: { activo: nil}) }


  has_one :relactivo, class_name: "Relactivo", foreign_key: "Activo"


end

和我的模特&#34; relactivo&#34;

class Relactivo < ActiveRecord::Base
  self.primary_key = "IdRow"
  belongs_to :activo, class_name:"Activo", foreign_key: "Activo"

end

1 个答案:

答案 0 :(得分:2)

尝试为您的范围执行此操作:

class WebAPI {
    private $mongoClient;
    function __construct() {
        $mongoClient = new MongoDB\Client("mongodb://database.local:27017");
        if(!$mongoClient){
            //TODO: Inform status site about failure
            http_response_code(500);
            die();
        }
        $statsDB = $mongoClient->stats; //selects database stats
        $pageViewsCollection = $statsDB->pageviews;
        $result = $pageViewsCollection->insertOne(PageViewInsights::getInfo());
        var_dump($result->getInsertedId());
    }
}

看看这是否有效,自定义主/外键使它有点奇怪,但试试这个。