I have hierarchical structure for model Board
(implemented using ancestry gem).
Instead of one model and some scopes, I'd like to have two models: Board
for root level elements (ancestry
column value is nil
), and Category
for the rest(ancestry
column value is not nil
). They would be using the same table boards
.
How can I do something like this?
You could specify the table name of the category model and generate a default scope:
class Category < ActiveRecord::Base
self.table_name = "boards"
default_scope where('boards.ancestry IS NOT NULL')
end
And you should be able to interact with both models wit the boards-Table.
Or you stay with one model and add two modules for the specific stuff. That depends on your preferences.
You can explicity define a table for a model using set_table_name
or self.table_name
depending on your rails version. Also you can define a default scope for every query made for this model, using default_scope
, so a combination of both should be what you are searching for:
class Category < AR:Base
self.table_name = 'boards'
default_scope where('boards.ancestry IS NOT NULL')
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With