... | @@ -30,7 +30,7 @@ end |
... | @@ -30,7 +30,7 @@ end |
|
|
|
|
|
### Models
|
|
### Models
|
|
|
|
|
|
In other to have your models working correctly and take fully advantage of this feature, the same way the tables are inherited, the models need to be inherited as well.
|
|
In other to have your models working correctly and take full advantage of this feature, the same way the tables are inherited, the models need to be inherited as well.
|
|
|
|
|
|
```ruby
|
|
```ruby
|
|
# models/activity.rb
|
|
# models/activity.rb
|
... | @@ -48,11 +48,11 @@ end |
... | @@ -48,11 +48,11 @@ end |
|
|
|
|
|
#### Table name to Model name
|
|
#### Table name to Model name
|
|
|
|
|
|
Since the data that indicates the record type is a table name, this process rely on rails common translation from class name to table name. But this is tricky, because `activity_posts` can either be `ActivityPost` or `Activity::Post` (or even another model using `self.table_name = "activity_posts"`). This GEM try its best to translate the table name to a model name, chicking all the possibilities, so you might not face issues. **But**, you can always help it, and make the process more accurate or even faster.
|
|
Since the data that indicates the record type is a table name, this process relies on rails common translation from class name to table name. But this is tricky, because `activity_posts` can either be `ActivityPost` or `Activity::Post` (or even another model using `self.table_name = "activity_posts"`). This GEM tries its best to translate the table name to a model name, chicking all the possibilities, so you might not face issues. **But**, you can always help it, and make the process more accurate or even faster.
|
|
|
|
|
|
Please, check the [Configuration Page](https://github.com/crashtech/torque-postgresql/wiki/Configuring) in order to see the options to improve this operation.
|
|
Please, check the [Configuration Page](https://github.com/crashtech/torque-postgresql/wiki/Configuring) in order to see the options to improve this operation.
|
|
|
|
|
|
The most important setting is the [`irregular_models`](https://github.com/crashtech/torque-postgresql/wiki/Configuring#irregular_models), which can both help descibing the correct relationship between a table name and a model name, and improve the performance by avoinding the default behavior of searching for the model based on the table name. Although the table name once associated with an model name is cahced, be aware of this for setting irregular names.
|
|
The most important setting is the [`irregular_models`](https://github.com/crashtech/torque-postgresql/wiki/Configuring#irregular_models), which can both help to describe the correct relationship between a table name and a model name and improve the performance by avoiding the default behavior of searching for the model based on the table name. Although the table name once associated with a model name is cached, be aware of this for setting irregular names.
|
|
|
|
|
|
```ruby
|
|
```ruby
|
|
Torque::PostgreSQL.configure do |c|
|
|
Torque::PostgreSQL.configure do |c|
|
... | @@ -66,7 +66,7 @@ end |
... | @@ -66,7 +66,7 @@ end |
|
|
|
|
|
#### Single record
|
|
#### Single record
|
|
|
|
|
|
Any already loaded record can be casted to its original model using `cast_record` method. **Be careful** that this method rely on a `primary_key` to be correctly casted.
|
|
Any already loaded record can be casted to its original model using `cast_record` method. **Be careful** that this method relies on a `primary_key` to be correctly casted.
|
|
|
|
|
|
```ruby
|
|
```ruby
|
|
ActivityPost.create(title: 'Post 1')
|
|
ActivityPost.create(title: 'Post 1')
|
... | @@ -76,7 +76,7 @@ Activity.first.cast_record # #<ActivityPost id: 1, title: "Post |
... | @@ -76,7 +76,7 @@ Activity.first.cast_record # #<ActivityPost id: 1, title: "Post |
|
|
|
|
|
#### Multiple records
|
|
#### Multiple records
|
|
|
|
|
|
You can also inform the relation to automatically cast all the returned recors to their original model using `cast_records` while querying.
|
|
You can also inform the relation to automatically cast all the returned records to their original model using `cast_records` while querying.
|
|
|
|
|
|
```ruby
|
|
```ruby
|
|
Activity.create(title: 'Activity 1')
|
|
Activity.create(title: 'Activity 1')
|
... | @@ -89,7 +89,7 @@ list.second # #<ActivityPost id: 2, title: "Post |
... | @@ -89,7 +89,7 @@ list.second # #<ActivityPost id: 2, title: "Post |
|
list.third # #<ActivityBook id: 3, title: "Book 1" ...
|
|
list.third # #<ActivityBook id: 3, title: "Book 1" ...
|
|
```
|
|
```
|
|
|
|
|
|
The `cast_records` provides other filters like specifying which classes shoul be casted and if they shold be filtered while querying.
|
|
The `cast_records` provides other filters like specifying which classes should be casted and if they should be filtered while querying.
|
|
|
|
|
|
```ruby
|
|
```ruby
|
|
# No filter applied
|
|
# No filter applied
|
... | @@ -107,7 +107,7 @@ list.third # nil |
... | @@ -107,7 +107,7 @@ list.third # nil |
|
|
|
|
|
#### Non confliting records
|
|
#### Non confliting records
|
|
|
|
|
|
With this feature, another method was introduced. The `itself_only` only allows queries on the base table to not include any inherited record. It uses the `FROM ONLY` SQL clause, so it's very performatic.
|
|
With this feature, another method was introduced. The `itself_only` only allows queries on the base table to not include any inherited record. It uses the `FROM ONLY` SQL clause, so it's very performative.
|
|
|
|
|
|
```ruby
|
|
```ruby
|
|
list = Activity.itself_only.load.to_a
|
|
list = Activity.itself_only.load.to_a
|
... | @@ -120,13 +120,11 @@ list.third # nil |
... | @@ -120,13 +120,11 @@ list.third # nil |
|
|
|
|
|
In order to perform any query with correct performance and maintainability, this feature uses [Auxiliary Statements](https://github.com/crashtech/torque-postgresql/wiki/Auxiliary-Statements) and [Dynamic Attributes](https://github.com/crashtech/torque-postgresql/wiki/Dynamic-Attributes).
|
|
In order to perform any query with correct performance and maintainability, this feature uses [Auxiliary Statements](https://github.com/crashtech/torque-postgresql/wiki/Auxiliary-Statements) and [Dynamic Attributes](https://github.com/crashtech/torque-postgresql/wiki/Dynamic-Attributes).
|
|
|
|
|
|
The `_record_class` (with can be renames using the [`inheritance.record_class_column_name`](https://github.com/crashtech/torque-postgresql/wiki/Configuring#inheritance.record_class_column_name) setting) is used both as an Auxiliary Statement and a Dynamic Attribute to get the type of the records. While it's a great example of these provided features, you can always take advantake of this to get the type of the record.
|
|
The `_record_class` (with can be renamed using the [`inheritance.record_class_column_name`](https://github.com/crashtech/torque-postgresql/wiki/Configuring#inheritance.record_class_column_name) setting) is used both as an Auxiliary Statement and a Dynamic Attribute to get the type of the records. While it's a great example of these provided features, you can always take advantage of this to get the type of the record.
|
|
|
|
|
|
```ruby
|
|
```ruby
|
|
list = Activity.all.with(:_record_class).load.to_a
|
|
list = Activity.all.with(:_record_class).load.to_a
|
|
list.first._record_class # "activities"
|
|
list.first._record_class # "activities"
|
|
list.second._record_class # "activity_posts"
|
|
list.second._record_class # "activity_posts"
|
|
list.third._record_class # "activity_books"
|
|
list.third._record_class # "activity_books"
|
|
```
|
|
``` |
|
|
|
\ No newline at end of file |
|
This will be improved in the next `v0.2.1` to accept an `type_activity?`. |
|
|
|
\ No newline at end of file |
|
|