Join 如何在squel中使用条件连接子查询 开场白

Join 如何在squel中使用条件连接子查询 开场白,join,subquery,squeel,Join,Subquery,Squeel,我拥抱了斯奎尔——享受每一步!非常感谢您的分享,厄尼·米勒 我正在使用ruby 1.9.2、Squel 1.0.2和Rails 3.2.5进行开发 (我承认,我已经对这个问题进行了彻底的重组——希望增加可读性,增加我得到答案的机会)Walt 你可能把这件事弄得太复杂了。如果我读对了,子查询的主要目的是获取结果中可用授权的串联列表。如果是这种情况,您可以简单地加载授权并通过角色模型上的方法公开它们的名称,该方法为您进行连接。它的第二个优点是与MySQL以外的DBs兼容。Walt 你可能把这件事弄得

我拥抱了斯奎尔——享受每一步!非常感谢您的分享,厄尼·米勒

我正在使用ruby 1.9.2、Squel 1.0.2和Rails 3.2.5进行开发

(我承认,我已经对这个问题进行了彻底的重组——希望增加可读性,增加我得到答案的机会)Walt

你可能把这件事弄得太复杂了。如果我读对了,子查询的主要目的是获取结果中可用授权的串联列表。如果是这种情况,您可以简单地加载授权并通过角色模型上的方法公开它们的名称,该方法为您进行连接。它的第二个优点是与MySQL以外的DBs兼容。

Walt


你可能把这件事弄得太复杂了。如果我读对了,子查询的主要目的是获取结果中可用授权的串联列表。如果是这种情况,您可以简单地加载授权并通过角色模型上的方法公开它们的名称,该方法为您进行连接。它的第二个优点是与MySQL以外的DBs兼容。

如我所说-最好使用squel:)

事实证明,(从马口中可以说)连接是斯奎尔县的协会;)

那怎么办呢?好吧,我用我的SQL做了最后一次环游,以ActiveRecord套索摆动,瞧!有人问了一个很好的问题——还有一个更伟大的答案!太好了

在短短的几分钟内,我用所描述的技术——和Heureka——砍掉了它

之前,我添加了一个pastiebin来帮助可能的“回答者”——因此我将结果添加到了中——但简而言之,它是这样的:

Model.select("something").joins("to your hearts contend")
干杯,
沃尔特

就像我说的那样-最好使用斯奎尔:)

事实证明,(从马口中可以说)连接是斯奎尔县的协会;)

那怎么办呢?好吧,我用我的SQL做了最后一次环游,以ActiveRecord套索摆动,瞧!有人问了一个很好的问题——还有一个更伟大的答案!太好了

在短短的几分钟内,我用所描述的技术——和Heureka——砍掉了它

之前,我添加了一个pastiebin来帮助可能的“回答者”——因此我将结果添加到了中——但简而言之,它是这样的:

Model.select("something").joins("to your hearts contend")
干杯,
沃尔特

你好,厄尼-谢谢你在这一次回复我!关于这个复杂的部分,你可能肯定是对的。我试图详细说明我的问题。你是否仍然认为这是一个“急切加载”的案例?也许我只是不喜欢在急切的负载上——在我的地盘上,EgyLyLoad看起来是什么样的?试图打败你:)我的UnIO授权方法现在被命名为X并具有定义-但是当我尝试最后一行:加入{RR}时,{ID==RR.Reababl Id},或者我尝试的任何东西,我每次都画空白:(不幸的是,您将无法在联接调用中引用子查询,因为联接是为关联保留的。但是,如前所述,如果您使用该子查询进行联接的主要原因是为了获得最后一列,即连接的授权列表,我建议将其分解为角色与关联的联接任何表格,然后包括{roleables.authorizations}。嗯-厄尼-有时你必须旅行去享受汗水回家;)嗨,厄尼-谢谢你回复我这个问题!关于复杂的部分,你可能肯定是对的。我试着详细说明我的问题。这是否仍然让你觉得是一个“急切的负载”案例?也许我只是不喜欢在急切的负载上——在我的地盘上,EgyLyLoad看起来是什么样子?试图打败你:)我的Un授权方法现在被命名为X并具有定义——但是当我尝试最后一行:加入{RR}时,{ID==RR.Reababl Id},或者我尝试的任何东西,我每次都画空白:(不幸的是,您将无法在联接调用中引用子查询,因为联接是为关联保留的。但是,如前所述,如果您使用该子查询进行联接的主要原因是为了获得最后一列,即连接的授权列表,我建议将其分解为角色与关联的联接任何表,然后包括{roleables.authorizations}。嗯-厄尼-有时你必须旅行才能享受回家的乐趣;)
class AbstractActionBase < ActiveRecord::Base
  self.abstract_class=true

  require 'tempfile'

  belongs_to :ox

  has_many :roleables, as: :roleable

  attr_accessible :ox_id

  validates_presence_of :ox_id

  #
  # all models inheriting from this will have versions
  has_paper_trail
  #
  #

  # 
  # Class method to providing for index SELECT's being married with roleables (permissions)
  # used from abstraction_actions_controller where build_collection calls this method 
  # the result 'should' be an ActiveRelation - used for the Kamanari 'result' call to readying pagination
  #
  def self.with_authorizations    

    # 
    # SELECT * FROM any_table at
    # left join (
    #     select r.roleable_id, r.roleable_type, group_concat( r.authorization )
    #   from roleables r
    #   where r.authorization is not null
    #   and r.roleable_id=at.id
    #   and r.roleable_type=at.base_class
    #   and r.role_id not in (1,2,3) <--- ID's are current_user.roles
    # ) rm on rm.roleable_id=at.id and rm.roleable_type=at.base_class
    #
    # which will provide for this:
    #
    # |.......| last column in table 'at' | roleable_id | roleable_type | authorizations |
    # |.......| some value                | 1           | 'UserGroup'   | 'insert,create'|
    # |.......| yet another value         | 92          | 'UserGroup'   | 'read'         |
    #
    #
    self.where{ active==true }
  end

  # compile a collection of records - regard search using Ransack
  def base.collection( params, resource_set )
    #
    # kaminari (and continous scrolling)
    #
    params[:page] ||= 1
    params[:per_page] ||= self.per_page
    params[:o] ||= self.resource_order_by
    distinct = params[:distinct].nil? ? false : params[:distinct].to_i.zero?
    resource_set = (resource_set.respond_to?( "result")) ? resource_set.result(:distinct => distinct) : resource_set
    (resource_set.respond_to?( "page")) ? resource_set.order(params[:o]).page( params[:page] ).per( params[:per_page] ) : resource_set.order(params[:o])
  end
end
class Role < AbstractActionBase

  has_many :roleables

  scope :active, where{ active.eq true }

  #
  # what does this role allow
  def permissions
    roleables.permissions.scoped
  end

  # 
  # to whom does this role allow
  def authorizations
    roleables.authorizations.scoped
  end

  # returns true if the roleables (permissions) authorizes the options
  # options are { controller: "", action: "", record: Instance, is_class: boolean }
  def authorizes?( options={} )
    coll = permissions
    coll = coll.on_action(options.delete(:action)) if options.keys.include? :action
    coll = coll.on_entity( options.delete(:record), options.delete(:is_class) || false ) if options.keys.include? :record
    coll = coll.on_controller(options.delete(:controller)) if options.keys.include? :controller
    (coll.count>0) === true
  end
end
class Roleable  < AbstractActionBase
  belongs_to :role
  belongs_to :roleable, polymorphic: true

  # roleables authorizes users through user_groups
  # (in which case the authorization is "-")
  # providing them permissions on controllers, actions and instances
  scope :authorizations, where{ authorization == nil }
  scope :permissions, where{ authorization != nil }

  # using Squeel, find roleables on a particular controller or any controller
  def self.on_controller(ctrl)
    where{ (controller==ctrl) | (controller==nil) }
  end

  # using Squeel, find roleables on a particular authorization or allowed 'all'
  def self.on_action(action)
    where{ (authorization=~ "%#{action}%") | (authorization=="all") }
  end

  # using Squeel, find roleables on a particular instance/record or class
  def self.on_entity(entity, is_class=false)
    if is_class
      where{ ((roleable_type==entity.base_class.to_s ) & ( roleable_id==nil)) | ((roleable_type==nil) & (roleable_id==nil)) }
    else
      where{ ((roleable_type==entity.class.to_s ) & ( roleable_id==entity.id)) | ((roleable_type==nil) & (roleable_id==nil)) }
    end
  end
end
class AbstractActionsController < InheritedResources::Base # < ApplicationController

  append_view_path ViewTemplate::Resolver.instance

  respond_to :html, :xml, :json, :js, :pdf

  belongs_to :ox, :optional => true

  before_filter :authorize!
  before_filter :authenticate!
  before_filter :warn_unless_confirmed!
  before_filter :fix_money_params, :only => [:create,:update]

  # GET /collection - printers
  def index

    # session[:params] = params
    #
    # preparing for Ransack
    unless params[:q].nil?
      params[:q]= { :"#{params[:q_fields]}" => params[:q] }
    end

    super do |format|
      format.html 
      format.js { render layout: false }
      format.pdf{ render :pdf => generate_pdf(false) and return }
      format.xml { render layout: false }
      format.json do
        # field lookup request?
        unless params[:lookup].nil?
          render layout: false, :json => collection.map(&:select_mapping)
        else
          render json: collection.map { |p| view_context.grow_mustache_for_index(p, collection, (parent? ? collection : resource_class.order(:id)), @selected ) }
        end
      end
    end
  end


  # the collection method on inherited_resources 
  # gets overloaded with Ransack search and Kaminari pagination (on the model)
  def collection
    # @collection ||= build_collection
    # TODO - test whether caching the collection is possible
    build_collection
  end

  def build_collection
    unless params[:belongs].nil?
      # debugger
      parent = params[:belongs].constantize.find(params[:belongs_id])
      @selected = parent.nil? ? [] : parent.send( rewrite_association(params[:assoc],parent) )
      @search_resource = core_entity(params[:assoc].constantize)
      @search_resource = @search_resource.search(params[:q]) unless params[:q].nil?
    else
      @search_resource = rewrite_end_of_association_chain(resource_class)
      @search_resource = core_entity(@search_resource)
      @search_resource = @search_resource.search(params[:q]) unless params[:q].nil?
    end
    # authorize rows
    @search_resource = @search_resource.with_authorizations                 # left joins roleables coalescing a "authorization" field from roles ID's not owned by current_user through his user_groups
    @resources ||= resource_class.collection( params, @search_resource )
  end

end
Model.select("something").joins("to your hearts contend")