How can you write cancan ability so can? and access_by both work on self-regulating the HABTM relationship?

I don’t understand how to deal with this situation using the HABTM relationship associated with link, cancan and ActiveRecord.

I am trying to use available_by to determine the set of videos that are visible given the relationship between the videos and the channels, but as a result, SQL has the wrong table name for the query part. Here are the ratios:

# video.rb
class Video < ActiveRecord::Base
  belongs_to :channel
end

# channel.rb
class Channel < ActiveRecord::Base
  has_many :videos
  has_and_belongs_to_many :subchannels, :class_name => "Channel", :join_table => "channels_channels", :foreign_key => :parent_id, :association_foreign_key => :subchannel_id
  has_and_belongs_to_many :parent_channels, :class_name => "Channel", :join_table => "channels_channels", :foreign_key => :subchannel_id, :association_foreign_key => :parent_id
end

# The appropriate channels_channels table exists with subchannel_id and parent_id fields.

I need the cancan ability to find all public videos that are in the public channel subchannels of the default channel. I tried the following:

# ability.rb
can :read, Video, :permission => 'public', :channel => {:permission => 'public', :parent_channels => {:name => "Default"}}

In the console, when I try this, I get the following:

> Video.accessible_by(Ability.new(nil))
SELECT "videos".* FROM "videos" INNER JOIN "channels" ON "channels"."id" = "videos"."channel_id" INNER JOIN "channels_channels" ON "channels_channels"."subchannel_id" = "channels"."id" INNER JOIN "channels" "parent_channels_channels" ON "parent_channels_channels"."id" = "channels_channels"."parent_id" WHERE "videos"."permission" = 'public' AND "channels"."permission" = 'public' AND "channels"."name" = 'Default'
=> [] 

, , , , , "parent_channels_channels". "name" = 'Default', "" , , SQL , .

, :

    can :read, Video, :permission => 'public', :channel => {:permission => 'public', :parent_channels => {"parent_channels_channels.name" => "Default"}}

, _by , ? (: , ) .

NoMethodError: undefined method `parent_channels_channels.name' for #<Channel:0x007fef35593748>

, ?, _by?

+5
1

, , . , parent_channels HABTM ? , , , , , HABTM , has_many .

VideoCategory has_many: videos,: through = > : channels.

class VideoCategory
  has_and_belongs_to_many :channels

  has_many :videos, :through => :channels
end

class Video
  belongs_to :channel
end

class Channel
  has_many :videos

  has_and_belongs_to_many :video_cateogories
end

, . habtm. , , 1--. , ( , , ).

, ?

class Video
  scope :public, lambda do
    joins(:channel).where("videos.permission = 'public' and channels.permission = 'public'")
  end
end

, . rb , SQL , - , CanCan.

0

All Articles