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:
class Video < ActiveRecord::Base
belongs_to :channel
end
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
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:
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?