Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Sequel: The Database Toolkit for Ruby

NotificationsYou must be signed in to change notification settings

jeremyevans/sequel

Repository files navigation

Sequel is a simple, flexible, and powerful SQL database access toolkit for Ruby.

  • Sequel provides thread safety, connection pooling and a concise DSL for constructing SQL queries and table schemas.

  • Sequel includes a comprehensive ORM layer for mapping records to Ruby objects and handling associated records.

  • Sequel supports advanced database features such as prepared statements, bound variables, savepoints, two-phase commit, transaction isolation, primary/replica configurations, and database sharding.

  • Sequel currently has adapters for ADO, Amalgalite, IBM_DB, JDBC, MySQL, Mysql2, ODBC, Oracle, PostgreSQL, SQLAnywhere, SQLite3, TinyTDS, and Trilogy.

Resources

Website

sequel.jeremyevans.net

RDoc Documentation

sequel.jeremyevans.net/rdoc

Source Code

github.com/jeremyevans/sequel

Bug tracking (GitHub Issues)

github.com/jeremyevans/sequel/issues

Discussion Forum (GitHub Discussions)

github.com/jeremyevans/sequel/discussions

Archived Discussion Forum (sequel-talk Google Group)

www.mail-archive.com/sequel-talk@googlegroups.com/

If you have questions about how to use Sequel, please ask on GitHub Discussions. Only use the the bug tracker to report bugs in Sequel, not to ask for help on using Sequel.

To check out the source code:

git clone git://github.com/jeremyevans/sequel.git

Contact

If you have any comments or suggestions please post to the Google group.

Installation

geminstallsequel

A Short Example

require'sequel'DB =Sequel.sqlite# memory database, requires sqlite3DB.create_table:itemsdoprimary_key:idString:nameFloat:priceenditems =DB[:items]# Create a dataset# Populate the tableitems.insert(name:'abc',price:rand*100)items.insert(name:'def',price:rand*100)items.insert(name:'ghi',price:rand*100)# Print out the number of recordsputs"Item count: #{items.count}"# Print out the average priceputs"The average price is: #{items.avg(:price)}"

The Sequel Console

Sequel includes an IRB console for quick access to databases (usually referred to asbin/sequel). You can use it like this:

sequel sqlite://test.db # test.db in current directory

You get an IRB session with the Sequel::Database object stored in DB.

In addition to providing an IRB shell (the default behavior), bin/sequel also has support for migrating databases, dumping schema migrations, and copying databases. See the bin/sequel guide for more details.

An Introduction

Sequel is designed to take the hassle away from connecting to databases and manipulating them. Sequel deals with all the boring stuff like maintaining connections, formatting SQL correctly and fetching records so you can concentrate on your application.

Sequel uses the concept of datasets to retrieve data. A Dataset object encapsulates an SQL query and supports chainability, letting you fetch data using a convenient Ruby DSL that is both concise and flexible.

For example, the following one-liner returns the average GDP for countries in the middle east region:

DB[:countries].where(region:'Middle East').avg(:GDP)

Which is equivalent to:

SELECT avg(GDP) FROM countries WHERE region = 'Middle East'

Since datasets retrieve records only when needed, they can be stored and later reused. Records are fetched as hashes, and are accessed using anEnumerable interface:

middle_east =DB[:countries].where(region:'Middle East')middle_east.order(:name).each{|r|putsr[:name]}

Sequel also offers convenience methods for extracting data from Datasets, such as an extendedmap method:

middle_east.map(:name)# => ['Egypt', 'Turkey', 'Israel', ...]middle_east.map([:id,:name])# => [[1, 'Egypt'], [3, 'Turkey'], [2, 'Israel'], ...]

Or getting results as a hash viaas_hash, with one column as key and another as value:

middle_east.as_hash(:name,:area)# => {'Israel' => 20000, 'Turkey' => 120000, ...}

Getting Started

Connecting to a database

To connect to a database you simply provideSequel.connect with a URL:

require'sequel'DB =Sequel.connect('sqlite://blog.db')# requires sqlite3

The connection URL can also include such stuff as the user name, password, and port:

DB =Sequel.connect('postgres://user:password@host:port/database_name')# requires pg

You can also specify optional parameters, such as the connection pool size, or loggers for logging SQL queries:

DB =Sequel.connect("postgres://user:password@host:port/database_name",max_connections:10,logger:Logger.new('log/db.log'))

It is also possible to use a hash instead of a connection URL, but make sure to include the :adapter option in this case:

DB =Sequel.connect(adapter::postgres,user:'user',password:'password',host:'host',port:port,database:'database_name',max_connections:10,logger:Logger.new('log/db.log'))

You can specify a block to connect, which will disconnect from the database after it completes:

Sequel.connect('postgres://user:password@host:port/database_name'){|db|db[:posts].delete}

The DB convention

Throughout Sequel’s documentation, you will see theDB constant used to refer to the Sequel::Database instance you create. This reflects the recommendation that for an app with a single Sequel::Database instance, the Sequel convention is to store the instance in theDB constant. This is just a convention, it’s not required, but it is recommended.

Note that some frameworks that use Sequel may create the Sequel::Database instance for you, and you might not know how to access it. In most cases, you can access the Sequel::Database instance throughSequel::Model.db.

Arbitrary SQL queries

You can execute arbitrary SQL code usingDatabase#run:

DB.run("create table t (a text, b text)")DB.run("insert into t values ('a', 'b')")

You can also create datasets based on raw SQL:

dataset =DB['select id from items']dataset.count# will return the number of records in the result setdataset.map(:id)# will return an array containing all values of the id column in the result set

You can also fetch records with raw SQL through the dataset:

DB['select * from items'].eachdo|row|prowend

You can use placeholders in your SQL string as well:

name ='Jim'DB['select * from items where name = ?',name].eachdo|row|prowend

Getting Dataset Instances

Datasets are the primary way records are retrieved and manipulated. They are generally created via theDatabase#from orDatabase#[] methods:

posts =DB.from(:posts)posts =DB[:posts]# same

Datasets will only fetch records when you tell them to. They can be manipulated to filter records, change ordering, join tables, etc. Datasets are always frozen, and they are safe to use by multiple threads concurrently.

Retrieving Records

You can retrieve all records by using theall method:

posts.all# SELECT * FROM posts

Theall method returns an array of hashes, where each hash corresponds to a record.

You can also iterate through records one at a time usingeach:

posts.each{|row|prow}

Or perform more advanced stuff:

names_and_dates =posts.map([:name,:date])old_posts,recent_posts =posts.partition{|r|r[:date]<Date.today-7}

You can also retrieve the first record in a dataset:

posts.order(:id).first# SELECT * FROM posts ORDER BY id LIMIT 1

Note that you can get the first record in a dataset even if it isn’t ordered:

posts.first# SELECT * FROM posts LIMIT 1

If the dataset is ordered, you can also ask for the last record:

posts.order(:stamp).last# SELECT * FROM posts ORDER BY stamp DESC LIMIT 1

You can also provide a filter when asking for a single record:

posts.first(id:1)# SELECT * FROM posts WHERE id = 1 LIMIT 1

Or retrieve a single value for a specific record:

posts.where(id:1).get(:name)# SELECT name FROM posts WHERE id = 1 LIMIT 1

Filtering Records

The most common way to filter records is to provide a hash of values to match towhere:

my_posts =posts.where(category:'ruby',author:'david')# WHERE ((category = 'ruby') AND (author = 'david'))

You can also specify ranges:

my_posts =posts.where(stamp: (Date.today-14)..(Date.today-7))# WHERE ((stamp >= '2010-06-30') AND (stamp <= '2010-07-07'))

Or arrays of values:

my_posts =posts.where(category: ['ruby','postgres','linux'])# WHERE (category IN ('ruby', 'postgres', 'linux'))

By passing a block to where, you can use expressions (this is fairly “magical”):

my_posts =posts.where{stamp>Date.today<<1}# WHERE (stamp > '2010-06-14')my_posts =posts.where{stamp=~Date.today}# WHERE (stamp = '2010-07-14')

If you want to wrap the objects yourself, you can use expressions without the “magic”:

my_posts =posts.where(Sequel[:stamp]>Date.today<<1)# WHERE (stamp > '2010-06-14')my_posts =posts.where(Sequel[:stamp]=~Date.today)# WHERE (stamp = '2010-07-14')

Some databases such as PostgreSQL and MySQL also support filtering via Regexps:

my_posts =posts.where(category:/ruby/i)# WHERE (category ~* 'ruby')

You can also use an inverse filter viaexclude:

my_posts =posts.exclude(category: ['ruby','postgres','linux'])# WHERE (category NOT IN ('ruby', 'postgres', 'linux'))

But note that this does a full inversion of the filter:

my_posts =posts.exclude(category: ['ruby','postgres','linux'],id:1)# WHERE ((category NOT IN ('ruby', 'postgres', 'linux')) OR (id != 1))

If at any point you want to use a custom SQL fragment for part of a query, you can do so viaSequel.lit:

posts.where(Sequel.lit('stamp IS NOT NULL'))# WHERE (stamp IS NOT NULL)

You can safely interpolate parameters into the custom SQL fragment by providing them as additional arguments:

author_name ='JKR'posts.where(Sequel.lit('(stamp < ?) AND (author != ?)',Date.today-3,author_name))# WHERE ((stamp < '2010-07-11') AND (author != 'JKR'))

Datasets can also be used as subqueries:

DB[:items].where(Sequel[:price]>DB[:items].select{avg(price)+100})# WHERE (price > (SELECT avg(price) + 100 FROM items))

After filtering, you can retrieve the matching records by using any of the retrieval methods:

my_posts.each{|row|prow}

See the Dataset Filtering file for more details.

Security

Designing apps with security in mind is a best practice. Please read the Security Guide for details on security issues that you should be aware of when using Sequel.

Summarizing Records

Counting records is easy usingcount:

posts.where(Sequel.like(:category,'%ruby%')).count# SELECT COUNT(*) FROM posts WHERE (category LIKE '%ruby%' ESCAPE '\')

And you can also query maximum/minimum values viamax andmin:

max =DB[:history].max(:value)# SELECT max(value) FROM historymin =DB[:history].min(:value)# SELECT min(value) FROM history

Or calculate a sum or average viasum andavg:

sum =DB[:items].sum(:price)# SELECT sum(price) FROM itemsavg =DB[:items].avg(:price)# SELECT avg(price) FROM items

Ordering Records

Ordering datasets is simple usingorder:

posts.order(:stamp)# ORDER BY stampposts.order(:stamp,:name)# ORDER BY stamp, name

order always overrides the existing order:

posts.order(:stamp).order(:name)# ORDER BY name

If you would like to add to the existing order, useorder_append ororder_prepend:

posts.order(:stamp).order_append(:name)# ORDER BY stamp, nameposts.order(:stamp).order_prepend(:name)# ORDER BY name, stamp

You can also specify descending order:

posts.reverse_order(:stamp)# ORDER BY stamp DESCposts.order(Sequel.desc(:stamp))# ORDER BY stamp DESC

Core Extensions

Note the use ofSequel.desc(:stamp) in the above example. Much of Sequel’s DSL uses this style, calling methods on the Sequel module that return SQL expression objects. Sequel also ships with a core_extensions extension that integrates Sequel’s DSL better into the Ruby language, allowing you to write:

:stamp.desc

instead of:

Sequel.desc(:stamp)

Selecting Columns

Selecting specific columns to be returned is also simple usingselect:

posts.select(:stamp)# SELECT stamp FROM postsposts.select(:stamp,:name)# SELECT stamp, name FROM posts

Likeorder,select overrides an existing selection:

posts.select(:stamp).select(:name)# SELECT name FROM posts

As you might expect, there areorder_append andorder_prepend equivalents forselect calledselect_append andselect_prepend:

posts.select(:stamp).select_append(:name)# SELECT stamp, name FROM postsposts.select(:stamp).select_prepend(:name)# SELECT name, stamp FROM posts

Deleting Records

Deleting records from the table is done withdelete:

posts.where(Sequel[:stamp]<Date.today-3).delete# DELETE FROM posts WHERE (stamp < '2010-07-11')

Be very careful when deleting, asdelete affects all rows in the dataset. Callwhere first anddelete second:

# DO THIS:posts.where(Sequel[:stamp]<Date.today-7).delete# NOT THIS:posts.delete.where(Sequel[:stamp]<Date.today-7)

Inserting Records

Inserting records into the table is done withinsert:

posts.insert(category:'ruby',author:'david')# INSERT INTO posts (category, author) VALUES ('ruby', 'david')

Updating Records

Updating records in the table is done withupdate:

posts.where(Sequel[:stamp]<Date.today-7).update(state:'archived')# UPDATE posts SET state = 'archived' WHERE (stamp < '2010-07-07')

You can provide arbitrary expressions when choosing what values to set:

posts.where(Sequel[:stamp]<Date.today-7).update(backup_number:Sequel[:backup_number]+1)# UPDATE posts SET backup_number = (backup_number + 1) WHERE (stamp < '2010-07-07'))))

As withdelete,update affects all rows in the dataset, sowhere first,update second:

# DO THIS:posts.where(Sequel[:stamp]<Date.today-7).update(state:'archived')# NOT THIS:posts.update(state:'archived').where(Sequel[:stamp]<Date.today-7)

Merging records

Merging records using the SQL MERGE statement is done usingmerge* methods. You usemerge_using to specify the merge source and join conditions. You can usemerge_insert,merge_delete, and/ormerge_update to set the INSERT, DELETE, and UPDATE clauses for the merge.merge_insert takes the same arguments asinsert, andmerge_update takes the same arguments asupdate.merge_insert,merge_delete, andmerge_update can all be called with blocks, to set the conditions for the related INSERT, DELETE, or UPDATE.

Finally, after calling all of the othermerge_* methods, you callmerge to run the MERGE statement on the database.

ds =DB[:m1]merge_using(:m2,i1::i2).merge_insert(i1::i2,a:Sequel[:b]+11).merge_delete{a>30}.merge_update(i1:Sequel[:i1]+:i2+10,a:Sequel[:a]+:b+20)ds.merge# MERGE INTO m1 USING m2 ON (i1 = i2)# WHEN NOT MATCHED THEN INSERT (i1, a) VALUES (i2, (b + 11))# WHEN MATCHED AND (a > 30) THEN DELETE# WHEN MATCHED THEN UPDATE SET i1 = (i1 + i2 + 10), a = (a + b + 20)

Transactions

You can wrap a block of code in a database transaction using theDatabase#transaction method:

DB.transactiondo# BEGINposts.insert(category:'ruby',author:'david')# INSERTposts.where(Sequel[:stamp]<Date.today-7).update(state:'archived')# UPDATEend# COMMIT

If the block does not raise an exception, the transaction will be committed. If the block does raise an exception, the transaction will be rolled back, and the exception will be reraised. If you want to rollback the transaction and not raise an exception outside the block, you can raise theSequel::Rollback exception inside the block:

DB.transactiondo# BEGINposts.insert(category:'ruby',author:'david')# INSERTifposts.where('stamp < ?',Date.today-7).update(state:'archived')==0# UPDATEraiseSequel::Rollbackendend# ROLLBACK

Joining Tables

Sequel makes it easy to join tables:

order_items =DB[:items].join(:order_items,item_id::id).where(order_id:1234)# SELECT * FROM items# INNER JOIN order_items ON (order_items.item_id = items.id)# WHERE (order_id = 1234)

The important thing to note here is that item_id is automatically qualified with the table being joined, and id is automatically qualified with the last table joined.

You can then do anything you like with the dataset:

order_total =order_items.sum(:price)# SELECT sum(price) FROM items# INNER JOIN order_items ON (order_items.item_id = items.id)# WHERE (order_id = 1234)

Note that the default selection in Sequel is*, which includes all columns in all joined tables. Because Sequel returns results as a hash keyed by column name symbols, if any tables have columns with the same name, this will clobber the columns in the returned hash. So when joining you are usually going to want to change the selection usingselect,select_all, and/orselect_append.

Column references in Sequel

Sequel expects column names to be specified using symbols. In addition, returned hashes always use symbols as their keys. This allows you to freely mix literal values and column references in many cases. For example, the two following lines produce equivalent SQL:

items.where(x:1)# SELECT * FROM items WHERE (x = 1)items.where(1=>:x)# SELECT * FROM items WHERE (1 = x)"

Ruby strings are generally treated as SQL strings:

items.where(x:'x')# SELECT * FROM items WHERE (x = 'x')

Qualifying identifiers (column/table names)

An identifier in SQL is a name that represents a column, table, or schema. The recommended way to qualify columns is to useSequel[][] orSequel.qualify

Sequel[:table][:column]Sequel.qualify(:table,:column)# table.column

You can also qualify tables with schemas:

Sequel[:schema][:table]# schema.table

or use multi-level qualification:

Sequel[:schema][:table][:column]# schema.table.column

Expression aliases

You can alias identifiers usingSequel[].as orSequel.as:

Sequel[:column].as(:alias)Sequel.as(:column,:alias)# column AS alias

You can use theSequel.as method to alias arbitrary expressions, not just identifiers:

Sequel.as(DB[:posts].select{max(id)},:p)# (SELECT max(id) FROM posts) AS p

And most Sequel expression objects support anas method for aliasing:

(Sequel[:column]+2).as(:c_plus_2)# (column + 2) AS c_plus_2

Sequel Models

A model class wraps a dataset, and an instance of that class wraps a single record in the dataset.

Model classes are defined as regular Ruby classes inheriting fromSequel::Model:

DB =Sequel.connect('sqlite://blog.db')classPost<Sequel::Modelend

When a model class is created, it parses the schema in the table from the database, and automatically sets up accessor methods for all of the columns in the table (Sequel::Model implements the active record pattern).

Sequel model classes assume that the table name is an underscored plural of the class name:

Post.table_name# => :posts

You can explicitly set the table name or even the dataset used:

classPost<Sequel::Model(:my_posts);end# or:classPost<Sequel::Model(DB[:my_posts]);end

If you pass a symbol to theSequel::Model method, it assumes you are referring to the table with the same name. You can also call it with a dataset, which will set the defaults for all retrievals for that model:

classPost<Sequel::Model(DB[:my_posts].where(category:'ruby'));endclassPost<Sequel::Model(DB[:my_posts].select(:id,:name).order(:date));end

Model instances

Model instances are identified by a primary key. Sequel queries the database to determine the primary key for each model. TheModel.[] method can be used to fetch records by their primary key:

post =Post[123]

Thepk method is used to retrieve the record’s primary key value:

post.pk# => 123

If you want to override which column(s) to use as the primary key, you can useset_primary_key:

classPost<Sequel::Modelset_primary_key [:category,:title]endpost =Post['ruby','hello world']post.pk# => ['ruby', 'hello world']

You can also define a model class that does not have a primary key viano_primary_key, but then you lose the ability to easily update and delete records:

Post.no_primary_key

A single model instance can also be fetched by specifying a condition:

post =Post.first(title:'hello world')post =Post.first{num_comments<10}

The dataset for a model class returns rows of model instances instead of plain hashes:

DB[:posts].first.class# => HashPost.first.class# => Post

Acts like a dataset

A model class forwards many methods to the underlying dataset. This means that you can use most of theDataset API to create customized queries that return model instances, e.g.:

Post.where(category:'ruby').each{|post|ppost}

You can also manipulate the records in the dataset:

Post.where{num_comments<7}.deletePost.where(Sequel.like(:title,/ruby/)).update(category:'ruby')

Accessing record values

A model instance stores its values as a hash with column symbol keys, which you can access directly via thevalues method:

post.values# => {:id => 123, :category => 'ruby', :title => 'hello world'}

You can read the record values as object attributes, assuming the attribute names are valid columns in the model’s dataset:

post.id# => 123post.title# => 'hello world'

If the record’s attributes names are not valid columns in the model’s dataset (maybe because you usedselect_append to add a computed value column), you can useModel#[] to access the values:

post[:id]# => 123post[:title]# => 'hello world'

You can also modify record values using attribute setters or the[]= method.

post.title ='hey there'post[:title] ='hey there'

That will just change the value for the object, it will not update the row in the database. To update the database row, call thesave method:

post.save

Mass assignment

You can also set the values for multiple columns in a single method call, using one of the mass-assignment methods. See the mass assignment guide for details. For exampleset updates the model’s column values without saving:

post.set(title:'hey there',updated_by:'foo')

andupdate updates the model’s column values and then saves the changes to the database:

post.update(title:'hey there',updated_by:'foo')

Creating new records

New model instances can be created by callingModel.new, which returns a new model instance without updating the database:

post =Post.new(title:'hello world')

You can save the record to the database later by callingsave on the model instance:

post.save

If you want to create a new record and save it to the database at the same time, you can useModel.create:

post =Post.create(title:'hello world')

You can also supply a block toModel.new andModel.create:

post =Post.newdo|p|p.title ='hello world'endpost =Post.create{|p|p.title ='hello world'}

Hooks

You can execute custom code when creating, updating, or deleting records by defining hook methods. Thebefore_create andafter_create hook methods wrap record creation. Thebefore_update andafter_update hook methods wrap record updating. Thebefore_save andafter_save hook methods wrap record creation and updating. Thebefore_destroy andafter_destroy hook methods wrap destruction. Thebefore_validation andafter_validation hook methods wrap validation. Example:

classPost<Sequel::Modeldefafter_createsuperauthor.increase_post_countenddefafter_destroysuperauthor.decrease_post_countendend

Note the use ofsuper if you define your own hook methods. Almost allSequel::Model class and instance methods (not just hook methods) can be overridden safely, but you have to make sure to callsuper when doing so, otherwise you risk breaking things.

For the example above, you should probably use a database trigger if you can. Hooks can be used for data integrity, but they will only enforce that integrity when you are modifying the database through model instances, and even then they are often subject to race conditions. It’s best to use database triggers and database constraints to enforce data integrity.

Deleting records

You can delete individual records by callingdelete ordestroy. The only difference between the two methods is thatdestroy invokesbefore_destroy andafter_destroy hook methods, whiledelete does not:

post.delete# => bypasses hookspost.destroy# => runs hooks

Records can also be deleted en-masse by callingdelete anddestroy on the model’s dataset. As stated above, you can specify filters for the deleted records:

Post.where(category:32).delete# => bypasses hooksPost.where(category:32).destroy# => runs hooks

Please note that ifdestroy is called, each record is deleted separately, butdelete deletes all matching records with a single SQL query.

Associations

Associations are used in order to specify relationships between model classes that reflect relationships between tables in the database, which are usually specified using foreign keys. You specify model associations via class methods:

classPost<Sequel::Modelmany_to_one:authorone_to_many:commentsone_to_one:first_comment,class::Comment,order::idmany_to_many:tagsone_through_one:first_tag,class::Tag,order::name,right_key::tag_idend

many_to_one andone_to_one create a getter and setter for each model object:

post =Post.create(name:'hi!')post.author =Author.first(name:'Sharon')post.author

one_to_many andmany_to_many create a getter method, a method for adding an object to the association, a method for removing an object from the association, and a method for removing all associated objects from the association:

post =Post.create(name:'hi!')post.commentscomment =Comment.create(text:'hi')post.add_comment(comment)post.remove_comment(comment)post.remove_all_commentstag =Tag.create(tag:'interesting')post.add_tag(tag)post.remove_tag(tag)post.remove_all_tags

Note that the remove_* and remove_all_* methods do not delete the object from the database, they merely disassociate the associated object from the receiver.

All associations add a dataset method that can be used to further filter or reorder the returned objects, or modify all of them:

# Delete all of this post's comments from the databasepost.comments_dataset.destroy# Return all tags related to this post with no subscribers, ordered by the tag's namepost.tags_dataset.where(subscribers:0).order(:name).all

Eager Loading

Associations can be eagerly loaded viaeager and the:eager association option. Eager loading is used when loading a group of objects. It loads all associated objects for all of the current objects in one query, instead of using a separate query to get the associated objects for each current object. Eager loading requires that you retrieve all model objects at once viaall (instead of individually byeach). Eager loading can be cascaded, loading association’s associated objects.

classPerson<Sequel::Modelone_to_many:posts,eager: [:tags]endclassPost<Sequel::Modelmany_to_one:personone_to_many:repliesmany_to_many:tagsendclassTag<Sequel::Modelmany_to_many:postsmany_to_many:repliesendclassReply<Sequel::Modelmany_to_one:personmany_to_one:postmany_to_many:tagsend# Eager loading via .eagerPost.eager(:person).all# eager is a dataset method, so it works with filters/orders/limits/etc.Post.where{topic>'M'}.order(:date).limit(5).eager(:person).allperson =Person.first# Eager loading via :eager (will eagerly load the tags for this person's posts)person.posts# These are equivalentPost.eager(:person,:tags).allPost.eager(:person).eager(:tags).all# Cascading via .eagerTag.eager(posts::replies).all# Will also grab all associated posts' tags (because of :eager)Reply.eager(person::posts).all# No depth limit (other than memory/stack), and will also grab posts' tags# Loads all people, their posts, their posts' tags, replies to those posts,# the person for each reply, the tag for each reply, and all posts and# replies that have that tag.  Uses a total of 8 queries.Person.eager(posts: {replies: [:person, {tags: [:posts,:replies]}]}).all

In addition to usingeager, you can also useeager_graph, which will use a single query to get the object and all associated objects. This may be necessary if you want to filter or order the result set based on columns in associated tables. It works with cascading as well, the API is similar. Note that usingeager_graph to eagerly load multiple*_to_many associations will cause the result set to be a cartesian product, so you should be very careful with your filters when using it in that case.

You can dynamically customize the eagerly loaded dataset by using a proc. This proc is passed the dataset used for eager loading, and should return a modified copy of that dataset:

# Eagerly load only replies containing 'foo'Post.eager(replies:proc{|ds|ds.where(Sequel.like(text,'%foo%'))}).all

This also works when usingeager_graph, in which case the proc is called with dataset to graph into the current dataset:

Post.eager_graph(replies:proc{|ds|ds.where(Sequel.like(text,'%foo%'))}).all

You can dynamically customize eager loads for botheager andeager_graph while also cascading, by making the value a single entry hash with the proc as a key, and the cascaded associations as the value:

# Eagerly load only replies containing 'foo', and the person and tags for those repliesPost.eager(replies: {proc{|ds|ds.where(Sequel.like(text,'%foo%'))}=> [:person,:tags]}).all

Joining with Associations

You can use theassociation_join method to add a join to the model’s dataset based on the association:

Post.association_join(:author)# SELECT * FROM posts# INNER JOIN authors AS author ON (author.id = posts.author_id)

This comes with variants for different join types:

Post.association_left_join(:replies)# SELECT * FROM posts# LEFT JOIN replies ON (replies.post_id = posts.id)

Similar to the eager loading methods, you can use multiple associations and nested associations:

Post.association_join(:author,replies::person).all# SELECT * FROM posts# INNER JOIN authors AS author ON (author.id = posts.author_id)# INNER JOIN replies ON (replies.post_id = posts.id)# INNER JOIN people AS person ON (person.id = replies.person_id)

Extending the underlying dataset

The recommended way to implement table-wide logic by defining methods on the dataset usingdataset_module:

classPost<Sequel::Modeldataset_moduledodefwith_few_commentswhere{num_comments<30}enddefclean_boringwith_few_comments.deleteendendend

This allows you to have access to your model API from filtered datasets as well:

Post.where(category:'ruby').clean_boring# DELETE FROM posts WHERE ((category = 'ruby') AND (num_comments < 30))

Insidedataset_module blocks, there are numerous methods that support easy creation of dataset methods. Most of these methods are named after the dataset methods themselves, such asselect,order, andgroup:

classPost<Sequel::Modeldataset_moduledowhere(:with_few_comments,Sequel[:num_comments]<30)select:with_title_and_date,:id,:title,:post_dateorder:by_post_date,:post_datelimit:top10,10endendPost.with_few_comments.with_title_and_date.by_post_date.top10# SELECT id, title, post_date# FROM posts# ORDER BY post_date# LIMIT 10

One advantage of using these methods inside dataset_module blocks, instead of defining methods manually, is that the created methods will generally cache the resulting values and result in better performance.

Model Validations

You can define avalidate method for your model, whichsave will check before attempting to save the model in the database. If an attribute of the model isn’t valid, you should add an error message for that attribute to the model object’serrors. If an object has any errors added by the validate method,save will raise an error by default:

classPost<Sequel::Modeldefvalidatesupererrors.add(:name,"can't be empty")ifname.empty?errors.add(:written_on,"should be in the past")ifwritten_on>=Time.nowendend

Testing Sequel

Please see the testing guide for recommendations on testing applications that use Sequel, as well as the how to run the tests for Sequel itself.

Sequel Release Policy

New major versions of Sequel do not have a defined release policy, but historically have occurred once every few years.

New minor versions of Sequel are released around once a month near the start of the month.

New tiny versions of Sequel are only released to address security issues or regressions in the most current release.

Ruby Support Policy

Sequel fully supports the currently supported versions of Ruby (MRI) and JRuby. It may support unsupported versions of Ruby or JRuby, but such support may be dropped in any minor version if keeping it becomes a support issue. The minimum Ruby version required to run the current version of Sequel is 1.9.2, and the minimum JRuby version is 9.2.0.0 (due to the bigdecimal dependency).

Maintainer

Jeremy Evans <code@jeremyevans.net>


[8]ページ先頭

©2009-2025 Movatter.jp