sequel

What are the possible solutions to OO/Table inheritance (ie. STI,MTI,CLI) in Rails 5+?

放肆的年华 提交于 2019-12-24 02:44:12
问题 These are the options I see that can help solve "Rails 5 - Object Relation Impedence and how to structure multiple inherited classes/tables" TL;DR - the Object Table Impedance ORM problem. Abstract base class, with each child class having its own table (ie storing the common attributes for each type in its own table) STI, just put everything in one Base class and Table, and create all child attributes in that table, but have the sub-classes (ie your other Rails models) inherit from that Base

Ruby: Outputting Sequel model associations

∥☆過路亽.° 提交于 2019-12-23 05:23:58
问题 I don't think this is possible using just Sequel models, but what I would like to do is have my parent model ( Author ) output its child model ( Book ) when I do something like Author.to_json . Here is my code: require 'sequel' require 'json' db = Sequel.connect('postgres://localhost/testing'); class Sequel::Model self.plugin :json_serializer end class Author < Sequel::Model(:author) one_to_many :book, key: :author_id, primary_key: :id def get_author Author.each do |e| books = Array.new e

Sequel (Ruby), how to increment and use a DB counter in a safe way?

半世苍凉 提交于 2019-12-22 08:19:12
问题 I found 4 "proper" ways to do this: In the cheat sheet for ActiveRecord users substitutes for ActiveRecord's increment and increment_counter are supposed to be album.values[:column] -= 1 # or += 1 for increment and album.update(:counter_name=>Sequel.+(:counter_name, 1)) In a SO solution update_sql is suggested for the same effect s[:query_volume].update_sql(:queries => Sequel.expr(3) + :queries) In a random thread I found this one dataset.update_sql(:exp => 'exp + 10'.lit) In the Sequels API

Transactions in Ruby Sequel module: how to get DB object?

跟風遠走 提交于 2019-12-22 00:26:59
问题 I'm working in a Sinatra application using Sequel. I want to make a transaction, according to the manual I have to use the DB object, how can I get this object from any part of my code? 回答1: You can define it in your base app.rb (or equivalent) or include a separate file where you configure the DB object if you wish. For example, in one of my Sinatra apps, I have an app.rb that includes a class App < Sinatra::Application #lots of stuff here... end require_relative 'models/init' In my models

Sequel: How to use group and count

蓝咒 提交于 2019-12-21 13:04:25
问题 Simply put, how can I do this query using Sequel? select a.id, count(t.id) from albums a right join tracks t on t.album_id = a.id group by a.id 回答1: DB[:albums___a]. right_join(:tracks___t, :album_id=>:id). select_group(:a__id). select_more{count(:t__id)} 回答2: Your problem is that the left join finds a track ID for each album ID. Solutions: right join subquery of sums, assuming sequel supports that: left join (select album_id, count(album_id) as count from tracks group by album_id) t on a

Running rake task from within war file

筅森魡賤 提交于 2019-12-20 05:52:29
问题 My code base initially was written in ruby. It had a rakefile.rb file to perform db migration. I later changed the whole thing to jruby for the ease of deployment which works fine. Only problem I am facing is how to run my rake task (to perform db migrations). I tried java -jar GV_S.war -S rake db_migrate[1] with 1 being the version but this didn't work. this gave me : [Winstone 2012/03/23 18:04:56] - Beginning extraction from war file [Winstone 2012/03/23 18:04:56] - WARNING: The Servlet 2.4

Slicing params hash for specific values

被刻印的时光 ゝ 提交于 2019-12-18 03:57:28
问题 Summary Given a Hash, what is the most efficient way to create a subset Hash based on a list of keys to use? h1 = { a:1, b:2, c:3 } # Given a hash... p foo( h1, :a, :c, :d ) # ...create a method that... #=> { :a=>1, :c=>3, :d=>nil } # ...returns specified keys... #=> { :a=>1, :c=>3 } # ...or perhaps only keys that exist Details The Sequel database toolkit allows one to create or update a model instance by passing in a Hash: foo = Product.create( hash_of_column_values ) foo.update( another

Can't join and select in Sequel — PG::SyntaxError

☆樱花仙子☆ 提交于 2019-12-14 03:23:53
问题 I'm trying to rename a column at join: # ............... result = DB[:my_items1].join(:my_items2, id: :my_item2_id). select([Sequel[:my_items2][:name].as(:my_items_name), Sequel[:my_items2][:amount].as(:my_item2_amount) ]) # ............... Exception: Sequel::DatabaseError - PG::SyntaxError: ERROR: syntax error at or near "AS" LINE 1: SELECT ("my_items2"."name" AS "my_item2_name", "... ^ : 回答1: Dataset#select takes multiple arguments, not a single array: result = DB[:my_items1].join(:my

How do I create a prepared insert statement in Sequel?

谁都会走 提交于 2019-12-13 17:27:14
问题 I am attempting to create a prepared insert statement in Sequel and I am as far as db[:registration].prepare(:insert) => <Sequel::Mysql2::Dataset/PreparedStatement "INSERT INTO `registration` () VALUES ()"> How do I create a statement that is something like the following: INSERT INTO `registration` (`name`, `email`) VALUES (?, ?) The documentation is a little bit obtuse and I can't find any examples online. 回答1: Figured this out looking at their rspecs: statement = db[:registration].prepare(

How to declare postgresql json/jsonb field with sequel?

你离开我真会死。 提交于 2019-12-13 08:40:08
问题 I'm stuck with that. Can't find anywhere in docs how to declare such spatial data types as inet and jsonb or json. ruby 2.4.1, sequel 4.47 Plain ruby script with require 'sequel' . Declaration such as DB.create_table :requests do primary_key :id foreign_key :client_id, :clients foreign_key :service_id, :services DateTime :created_at, null: false DateTime :answered_at, null: false JsonBType :request, null: false end 回答1: You can use the alternative way of defining columns by using the column