Help with defaultScope() AND HAS_MANY Relation

Hello,

I need help with a situation that can not resolve.

I have a model that represents the files and another that lists the roles that have permission to access the files.

I must apply the rule of permission and believe that the best way is using the DefaultScope ().

My code looks like:




        public function defaultScope()

        {

            return  array('with'=>array('filesRolesRelation'));

        }


        public function relations() {

			return array(

				'filesRolesRelation' =>  array(self::HAS_MANY, 'RelFileRoles', 'id_file', 'join'=>"AND filesRolesRelation.name IN ('Guest', 'Author')", 'joinType'=>'INNER JOIN'),

			);

		}



But I realize that the relationship is not applied correctly and the records are not filtered.

Queries generated on Grid:




SELECT COUNT(DISTINCT `t`.`id_file`) FROM `files` `t` INNER JOIN `rel_file_roles` `filesRolesRelation` ON (`filesRolesRelation`.`id_file`=`t`.`id_file`) AND filesRolesRelation.name IN ('Guest', 'Author')

SELECT `t`.`id_file` AS `t0_c0`, `t`.`filename` AS `t0_c1` FROM `files` `t` ORDER BY date LIMIT 20

SELECT `t`.`id_file` AS `t0_c0`, `filesRolesRelation`.`id_file` AS `t1_c0`, `filesRolesRelation`.`name` AS `t1_c1` FROM `files` `t` INNER JOIN `rel_file_roles` `filesRolesRelation` ON (`filesRolesRelation`.`id_file`=`t`.`id_file`) AND filesRolesRelation.name IN ('Guest', 'Author') WHERE (`t`.`id_file` IN (4, 14, 13, 12, 11, 10, 8, 7, 5, 15))



The records returned on the Grid are based in the second query.

Can anyone help me understand what is wrong or point me to a better solution?

Thanks!