ruby - How to write joins Rails ActiveRecord_Relation? -



ruby - How to write joins Rails ActiveRecord_Relation? -

> u = user.first > u.viewable_cars or > car.for(u)

would me cars user has permission view not cars owns! sql in irb both u.viewable_cars & car.for(u), same, cars id 1 50 belongs user don't called @ all:

select "cars".* "cars" inner bring together "permissions" on "permissions"."thing_id" = "cars"."id" , "permissions"."thing_type" = $1 ((cars.user_id = 1) or (permissions.action = 'view' , permissions.user_id = 1)) order created_at desc [["thing_type", "car"]] => #<activerecord::relation [#<car id: 52, content: "sport edition", name: "bmw", user_id: 2, created_at: "2014-11-01 04:58:19", updated_at: "2014-11-01 04:58:19">, #<car id: 51, content: "super sport car", name: "bmw m6", user_id: 3, created_at: "2014-11-01 04:44:31", updated_at: "2014-11-01 04:44:31">]> class auto < activerecord::base belongs_to :user has_many :permissions, as: :thing default_scope -> { order('created_at desc') } validates :user_id, presence: true validates :name, presence: true, length: { maximum: 50 }, uniqueness: true validates :content, length: { maximum: 300 }, allow_blank: true scope :viewable_by, ->(user) joins(:permissions).where(permissions: { action: "view", user_id: user.id }) end scope :for, ->(user) joins(:permissions). where("(cars.user_id = :user_id) or (permissions.action = 'view' , permissions.user_id = :user_id)", user_id: user.id) end end class user < activerecord::base ... has_many :cars, dependent: :destroy has_many :permissions has_many :viewable_cars, ->(user) { joins(:permissions). where("(cars.user_id = :user_id) or (permissions.action = 'view' , permissions.user_id = :user_id)", user_id: user.id) }, class_name: "car" def viewable_cars car.for(self) end end class permission < activerecord::base belongs_to :user belongs_to :thing, polymorphic: true end rails.application.routes.draw resources :users resources :cars end end

your scope for in car.rb should this:

scope :for, ->(user) joins("left outer bring together permissions on permissions.thing_id = cars.id , permissions.thing_type = 'car'").where("(cars.user_id = :user_id) or (permissions.action = 'view' , permissions.user_id = :user_id)", user_id: user.id) end

now can do: car.for(current_user).find(params[:id]). however, looks antipattern me. can create association in user.rb this:

rails 4:

has_many :viewable_cars, ->(user) { joins("left outer bring together permissions on permissions.thing_id = cars.id , permissions.thing_type = 'car'"). where("(cars.user_id = :user_id) or (permissions.action = 'view' , permissions.user_id = :user_id)", user_id: user.id) }, class_name: 'car'

rails 3(couldn't find improve way of doing it, e.g.: associations):

def viewable_cars car.for(self) end

so can fetch cars user:

current_user.viewable_cars

in controller:

@car = current_user.viewable_cars.find(params[:id])

ruby-on-rails ruby

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -