Replies: 6 comments 3 replies
-
You can do: MarketSequelQuery = Class.new(Sequel::Model)
MarketSequelQuery.class_eval do
singleton_class.attr_accessor :entry_level
# ...
end In terms of signaling a read-only model, I'm not sure exactly what you are looking for. Are you looking for a specific error to be raised if you attempt to save the model? Do you want to freeze all instances so they cannot be modified? |
Beta Was this translation helpful? Give feedback.
-
Hi jeremy, not sure I understand what you mean by that example (I'm a bit rusty on my ruby, sorry). class MarketCteDataset
def self.cte
entry_level = Sequel.lit("some-sql").as(:entry_level)
DB[:impessions_by_showing_level].with(
:impessions_by_showing_level,
DB[Sequel[:markets].as(:m)].select(
:id,
entry_level
).exclude(population: nil)
)
end
end And inheriting from that on the model class MarketSequelQuery < Sequel::Model(MarketCteDataset.cte)
end Is it possible to write a plugin for this kind of usecase? Not even sure if its possible to write such a plugin.
Yeah, it would be just to avoid any confusion to make sure to raise a relevant error in case somebody does class MarketSequelQuery < Sequel::Model(MarketCteDataset.cte)
def initialize(...)
raise 'Read only class (or something like that)'
end
end But thats a minor detail, my main question is about the base CTE and if I'm setting up the dataset the right way. |
Beta Was this translation helpful? Give feedback.
-
Personally, I would use a local variable in your case (the second code block in your original post). I don't understand what problem you are trying to solve by avoiding that. However, you shouldn't have a problem using the In terms of a read-only model, overriding |
Beta Was this translation helpful? Give feedback.
-
Thank you so much for the replies jeremy, I will go with the |
Beta Was this translation helpful? Give feedback.
-
I'm not sure if this is along the same lines, but I was wondering about something similar: I have a standard task-management setup (a On the database level, this is easy: @tasks = DB[:"tasks-#{@project.id}"] Can I archieve something similar with
|
Beta Was this translation helpful? Give feedback.
-
You can probably handle this via: one_to_many :tasks, instance_specific: true do |ds|
ds.from("tasks-#{id}")
end This doesn't change the model's dataset at runtime, it just changes the table used for the query. I would advise against such an approach, though. If you need an auto incrementing task number per project, one way to handle it is an insert trigger on tasks that sets the task number for the inserted row. Something like this, though the syntax will depend on your database: NEW.number := SELECT coalesce(max(number), 0) + 1 FROM tasks WHERE project_id = NEW.project_id; With the appropriate transaction isolation level, that should be fine. |
Beta Was this translation helpful? Give feedback.
-
Hi all. I'm running some experiments with sequel to try and replace a mess of raw sql string manipulation on one of my apps.
I found about
.with
to generate CTEs and it looks perfect for me because I can directly translate some of my CTE queries.So, by reading the docs I learned that I can pass a dataset to the base model class like so
In my case I tried something like this
I can use the model as
MarketSequelQuery.where(id: 799)
orMarketSequelQuery.all
and so on.But I think that creating the cte as an unscoped variable like that is kinda messy. Is there some api so I can setup the dataset more or less like this?
Also, do I need to setup something on the model to signal that this is a "read-only" model?
Beta Was this translation helpful? Give feedback.
All reactions