Why not use a computed property on the Post: <property name="Count" type="Int32" formular="select count(c.id) from Comments c where c.post_id = id" />
I have used this in a similar scenario with a web application, and it worked very well. The catch is that you have SQL in the mapping file, which will make them less portable to another database, but unless the system needs to switch database, I would not consider that a problem. Cheers, Thomas On 22 Okt., 22:49, jonnii <[EMAIL PROTECTED]> wrote: > It's a way of keeping the record count of an association in the parent > table. > > So if you had: > > posts > - id > - title > - body > - comment_count > > Comments > - id > - body > - post_id > > Then the comment count field for a post would contain the number of > posts. This means that if you did something like > > Post p = postsRepository.Find(3); > Console.WriteLine(p.Count); > > You wouldn't hit the database because the count on the association > would be loaded from the posts table. > > In AR there are hooks on the associations that increment and decrement > the counter cache if it's available. > > Does that make sense? > > Here's some more from AR's documentation: > > # :counter_cache - caches the number of belonging objects on the > associate class through the use of increment_counter and > decrement_counter. The counter cache is incremented when an object of > this class is created and decremented when it‘s destroyed. This > requires that a column named #{table_name}_count (such as > comments_count for a belonging Comment class) is used on the associate > class (such as a Post class). You can also specify a custom counter > cache column by providing a column name instead of a true/false value > to this option (e.g., :counter_cache => :my_custom_counter.) Note: > Specifying a counter_cache will add it to that model‘s list of > readonly attributes using attr_readonly. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "nhusers" group. To post to this group, send email to nhusers@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/nhusers?hl=en -~----------~----~----~----~------~----~------~--~---