[Catalyst] The Definitive Guide to Catalyst ... p. 165 listing all users and their roles

Tim Anderson tja824 at gmail.com
Wed Jul 11 18:57:59 GMT 2012


Sorry Robyn (and others I may have misled), I'm wrong.

I reviewed again, and you're not leveraging the many_to_many at all.
 You're following a path that looks like this:

[% FOREACH role =3D user.user_roles %]

User.pm has a 'has_many' relationship called 'user_roles' that you are
leveraging, in each iteration of this loop role is set to a a UserRole row
object.


   <li>[% role.role.role %]</li>

UserRole row object has 'belongs_to' relationship called 'role' (the second
'role' in the TT string above.  This is why fREW's answer works.
 Originally, using role_id accessed the value of role_id column in the
UserRole row, but you need to go one level deeper, so you use the
belongs_to 'role' to step to the related Role object row.
The related Role object has a value of role (the third 'role' in the TT
string), and this is the value that you want printed out.


[% END %]

I hope this helps.  I'm learning too.  I've taken a lot from this list, and
I'm trying to give back.


-Tim


On Wed, Jul 11, 2012 at 1:25 PM, Robyn Jonahs <learn.catalyst at gmail.com>wro=
te:

> Thanks Tim,
>
> I thought that the many_to_many was a relationship bridge and as limited
> to what it could do. I was studying the many_to_Many examples in the book
> to help my real problem in a test application I am trying to work on. I
> guess I should start a new thread for questions related to that. I will do
> that and try to continue understanding the relationships.
>
> Thanks
> RJ
>
> On Wed, Jul 11, 2012 at 12:48 PM, Tim Anderson <tja824 at gmail.com> wrote:
>
>>
>> <li>[% role.role.role %]</li>  works because you're accessing the role
>> name (the third 'role') through the 'role' accessor (the second 'role')
>> which is defined in your many-to-many relationship:
>>
>> __PACKAGE__->many_to_many("roles", "user_roles", *"role"*);
>>
>> This is one of the beauties of DBIC; as soon as you have your
>> relationships defined, you have paths to the data you need.  Imagine try=
ing
>> to write the same type of request across a multiple key relationship.
>>
>>
>> -Tim
>>
>>
>> On Wed, Jul 11, 2012 at 11:29 AM, Robyn Jonahs <learn.catalyst at gmail.com=
>wrote:
>>
>>> Thanks, that worked. Now I am off to see why.
>>>
>>> [ snip ]
>>>
>>
>> _______________________________________________
>> List: Catalyst at lists.scsys.co.uk
>> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
>> Searchable archive:
>> http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
>> Dev site: http://dev.catalyst.perl.org/
>>
>>
>
> _______________________________________________
> List: Catalyst at lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20120711/5edaa=
477/attachment.htm


More information about the Catalyst mailing list