Skip to content

Commit 240e8fb

Browse files
committed
Minor styling edits to README. Updates to CHANGELOG
* Polishes syntax highlighting on contributed README content from [Zoltan Paulovics](https://github.com/zpaulovics). * Updates CHANGELOG to document what is currently unreleased.
1 parent 6e6a775 commit 240e8fb

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

CHANGELOG.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# CHANGELOG
22

3+
## (yet) Unreleased
4+
* Fix count method to work with select statements under Rails 4.1. Thanks to [Jason Mitchell](https://github.com/mitchej123) for the contribution.
5+
* Edits to `README` documentation about the `options` hash. Thanks to [Jonathan E Hogue](https://github.com/hoguej) for pointing out that previous documentation was confusing and didn't address its usage properly.
6+
* Edits to `README` documentation on complex model queries inside the `get_raw_records` method. A round of applause to [Zoltan Paulovics](https://github.com/zpaulovics) for contributing this awesome piece of documentation. :smile:
7+
38
## 0.2.0
49
* This version works with jQuery dataTables ver. 1.10 and it's new API syntax.
510
* Added `legacy` branch to repo. If your project is working with jQuery
@@ -12,7 +17,7 @@
1217

1318
## 0.1.1
1419
* Fixes problem on `searchable_columns` where the corresponding model is
15-
a composite model name, e.g. `UserData`, `BillingAddress`.
20+
a composite model name, e.g. `UserData`, `BillingAddress`.
1621
Thanks to [iruca3](https://github.com/iruca3) for the fix.
1722

1823
## 0.1.0
@@ -25,8 +30,8 @@ Thanks to [iruca3](https://github.com/iruca3) for the fix.
2530
* Removes dependency to pass in a model name to the generator. This way,
2631
the developer has more flexibility to implement whatever datatable feature is required.
2732
* Datatable constructor accepts an optional `options` hash to provide
28-
more flexibility.
33+
more flexibility.
2934
See [README](https://github.com/antillas21/ajax-datatables-rails/blob/master/README.mds#options) for examples.
3035
* Sets generator inside the `Rails` namespace. To generate an
3136
`AjaxDatatablesRails` child class, just execute the
32-
generator like this: `$ rails generate datatable NAME`.
37+
generator like this: `$ rails generate datatable NAME`.

README.md

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ this method, will be chained (for now) to `ActiveRecord` methods for sorting, fi
154154
#### Associated and nested models
155155
The previous example has only one single model. But what about if you have some associated nested models and in a report you want to show fields from these tables.
156156

157-
Take an example that has an Event, Course, Coursetype, Allocation, Teacher, Contact, Competency and Competency_type models. We want to have a datatables report which has the following column:
157+
Take an example that has an `Event, Course, Coursetype, Allocation, Teacher, Contact, Competency and CompetencyType` models. We want to have a datatables report which has the following column:
158158
```ruby
159159
'coursetypes.name',
160160
'courses.name',
@@ -195,22 +195,34 @@ We want to sort and search on all columns of the list. The related definition wo
195195
end
196196

197197
def get_raw_records
198-
Event.joins([{course: :coursetype}, {allocations: {teacher: [:contact, {competencies: :competency_type}]}} ]).distinct
198+
Event.joins(
199+
{ course: :coursetype },
200+
{ allocations: {
201+
teacher: [:contact, {competencies: :competency_type}]
202+
}
203+
}).distinct
199204
end
200205
```
201206

202207
__Some comments for the above code:__
203208

204-
1. In the list we show full_name, but in sortable_columns and searchable_columns we use last_name from the Contact model. The reason is we can use only database columns as sort or search fields and the full_name is not a database field.
209+
1. In the list we show `full_name`, but in `sortable_columns` and `searchable_columns` we use `last_name` from the `Contact` model. The reason is we can use only database columns as sort or search fields and the full_name is not a database field.
205210

206-
2. In the get_raw_records method we have quite a complex query having one to many and may to many associations using the joins ActiveRecord method. The joins will generate INNER JOIN relations in the SQL query. In this case we do not include all event in the report if we have events which is not associated with any model record from the relation.
211+
2. In the `get_raw_records` method we have quite a complex query having one to many and may to many associations using the joins ActiveRecord method. The joins will generate INNER JOIN relations in the SQL query. In this case we do not include all event in the report if we have events which is not associated with any model record from the relation.
207212

208-
3. To have all event records in the list we should use the .includes method, which generate LEFT OUTER JOIN relation of the SQL query. __IMPORTANT:__ Make sure to append .references(:related_model) with any associated model. That forces the eager loading of all the associated models by one SQL query, and the search condition for any column works fine. Otherwise the :recordsFiltered => filter_records(get_raw_records).count(:all) will generate 2 SQL queries (one for the Event model, and then another for the associated tables). The :recordsFiltered => filter_records(get_raw_records).count(:all) will use only the first one to return from the ActiveRecord::Relation object in get_raw_records and you will get an error message of __Unknown column 'yourtable.yourfield' in 'where clause'__ in case the search field value is not empty.
213+
3. To have all event records in the list we should use the `.includes` method, which generate LEFT OUTER JOIN relation of the SQL query. __IMPORTANT:__ Make sure to append `.references(:related_model)` with any associated model. That forces the eager loading of all the associated models by one SQL query, and the search condition for any column works fine. Otherwise the `:recordsFiltered => filter_records(get_raw_records).count(:all)` will generate 2 SQL queries (one for the Event model, and then another for the associated tables). The `:recordsFiltered => filter_records(get_raw_records).count(:all)` will use only the first one to return from the ActiveRecord::Relation object in `get_raw_records` and you will get an error message of __Unknown column 'yourtable.yourfield' in 'where clause'__ in case the search field value is not empty.
209214

210-
So the query with includes() method is:
211-
```
215+
So the query using the `.includes()` method is:
216+
217+
```ruby
212218
def get_raw_records
213-
Event.includes([{course: :coursetype}, {allocations: {teacher: [:contact, {competencies: :competency_type}]}} ]).references(:course).distinct
219+
Event.includes(
220+
{ course: :coursetype },
221+
{ allocations: {
222+
teacher: [:contact, { competencies: :competency_type }]
223+
}
224+
}
225+
).references(:course).distinct
214226
end
215227
```
216228

0 commit comments

Comments
 (0)