You also need to add a :counter_cache=>true option to the relationship in
the Meetings model. The Meetings model would then look like this:
belongs_to :users, :foreign_key=>:assigned_user_id, :counter_cache=>true
CHAPTER 8 n CREATING SALES PERFORMANCE REPORTS WITH SUGARCRM 165
Some databases, like PostgreSQL and MySQL 5, support triggers. In that case, you may also be
able to use triggers to automatically update your counter cache whenever the associated table is
changed, even if it??™s changed by SugarCRM or another non-Rails application. For details, consult your
database documentation. You can also see a simple example of using MySQL triggers at http://www.
phpied.com/mysql-triggers/.
Also notice that this example does not have one large, custom SQL query, which is unlike many
of the other examples in this book. Calculating using custom SQL queries, rather than by using Active
Record methods, has two advantages: you can use the database to extract complicated information,
which would be harder to code in Ruby, and you can often make custom SQL queries that are faster
than the autogenerated queries that Active Record creates behind the scenes.
Pages:
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240