[% number_of_comments = user.user_comments_rs.count %]

* How do I use DBIx::Class objects in my TT templates?
Like normal objects, mostly. However you need to watch out for TT
calling methods in list context. When calling relationship accessors
you will not get resultsets, but a list of all the related objects.
Starting with version 0.07, you can use "search_rs" in
DBIx::Class::ResultSet to work around this issue.

This is DBIC FAQ ;)

2010/1/2 Octavian Râşniţă <orasn...@gmail.com>:
> Hi,
>
> I want to use a DBIx::Class resultset object in a TT template, but I am
> missing something and I can't get the correct value.
>
> I have a row object $user and I want to get the number of comments sent by
> that user from a related table named user_comment, using the relation
> user_comments. The perl code for doing this is:
>
> my $number_of_comments = $user->user_comments->count;
>
> In the TT template I tried to use:
>
> [% number_of_comments = user.user_comments.count %]
>
> But it doesn't print anything when I print the var number_of_comments.
>
> In this particular case I could get the entire array of comments and apply
> the .size vmethod, but it wouldn't work in all cases. For example, I would
> also like to get the votes for the current user, and I need to use a sum()
> method like:
>
> my $number_of_votes = $user->user_votes->get_column('vote')->sum;
>
> I have tried in TT:
>
> [% number_of_votes = user.user_votes.get_column('vote').sum %]
>
> But it doesn't work either.
>
> Please tell me what I am missing, because I think it should be possible to
> use these method calls in TT templates.
>
> Thank you.
>
> Octavian
>
>
> _______________________________________________
> templates mailing list
> templates@template-toolkit.org
> http://mail.template-toolkit.org/mailman/listinfo/templates
>



-- 
Sincerely yours,
Oleg Kostyuk (CUB-UANIC)

_______________________________________________
templates mailing list
templates@template-toolkit.org
http://mail.template-toolkit.org/mailman/listinfo/templates

Reply via email to