On Sunday, June 26, 2016 at 10:01:45 AM UTC-7, [email protected] wrote: > > In a rather complex application, I’ve been working on improving DB query > times/counts. > > In one occasion, I’ve used select_more to add specific values to the > dataset, that aren’t part of a specific model, but are used to prevent > multiple queries. For example: > > User.where(id: 1).select_append { Post.where(user_id: id).published.select { > count(:id).as(:posts_count) } }.first > > I know I could use a counter-cache column for this, but I don’t need this > available everywhere, only in this specific location of the application. > > The problem is, now I have an instance of a User, and the application > continues as expected, until a save is triggered, at which point Sequel > will actually try to store the posts_count value: > > PG::UndefinedColumn: ERROR: column "posts_count" of relation "users" does > not exist > > It might be important to note that I added a “convenience method” to the > User model like this: > > def posts_count > values[:posts_count] || posts_dataset.published.countend > > Is there a way to make sequel only save columns that are “real” column > attributes in the model’s table? Or is there some other way to solve this? > Important here is that this specific instance of save is called from > multiple paths within the code, so every other scenario, except the one > described above won’t have the posts_count value set, and thus still > works as expected. > Model#save supports a :columns option, so you can specify which columns you want to save: https://github.com/jeremyevans/sequel/blob/master/lib/sequel/model/base.rb#L1674
Alternatively, just do values.delete(:posts_count) in a before_update hook. Thanks, Jeremy > > -- You received this message because you are subscribed to the Google Groups "sequel-talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/sequel-talk. For more options, visit https://groups.google.com/d/optout.
