Re: Models with generated column fail to save

2021-10-28 Thread Adam Daniels
You want the skip_saving_columns plugin. It should handle this for you 
automatically.

https://sequel.jeremyevans.net/rdoc-plugins/classes/Sequel/Plugins/SkipSavingColumns.html

Adam

Sent from my iPhone

> On Oct 28, 2021, at 7:18 PM, Andrew Burleson  wrote:
> 
> I'm sure this is user error, but I'm having a surprising issue using 
> generated columns.
> 
> In short I can't save a record after creating it, because Sequel is trying to 
> write back the generated column, which is not allowed. The exception looks 
> like this:
> 
> .../sequel-5.48.0/lib/sequel/adapters/postgres.rb:156:in `exec': 
> PG::SyntaxError: ERROR:  column "full_name" can only be updated to DEFAULT 
> (Sequel::DatabaseError)
> DETAIL:  Column "full_name" is a generated column.
> 
> As you can guess from the error I have a user column with a first_name and 
> last_name column, and a generated column full_name. 
> 
> That was created via this line in the table creation:
> 
> column  :full_name,   String, generated_always_as: 
> Sequel[:first_name] + ' ' + Sequel[:last_name], generated_type: :stored
> 
> This works perfectly, except that after records are created they can't be 
> saved... The code below is enough to trigger the error.
> 
> user = User.first! email: 'f...@bar.com'
> user.save
> 
> I'm guessing there's some configuration I need to do on the model to tell it 
> not to write the full_name column, but I haven't been able to figure it out 
> yet. Any help is appreciated :)
> 
> Thank you!
> - Andrew
> -- 
> 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 sequel-talk+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sequel-talk/d9396bdc-f96b-4826-82c1-0c8cf5b95732n%40googlegroups.com.

-- 
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 sequel-talk+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/BE99BE1C-B83E-404D-A1C4-FDCFC817684F%40gmail.com.


Re: Mapping Tablename with Object

2020-02-03 Thread Adam Daniels
Inherit using the Sequel::Model method instead of the class. 

https://sequel.jeremyevans.net/rdoc/classes/Sequel/Model/ClassMethods.html#method-i-Model

Sent from my iPhone

> On Feb 3, 2020, at 7:03 PM, BeeRich33  wrote:
> 
> 
> I'm trying to rework an old test site using Sequel.  
> 
> I am using Sequel::Model and the class definitions with slightly abstract 
> table names don't match up.  
> 
> Is there a way I can force this recognition?  
> 
> class Biker with tablename biker_rt
> 
> Going through a whole site rebuilding relationships will be a major project 
> if this is the case, so I'm trying to avoid this.  
> 
> Cheers
> -- 
> 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 sequel-talk+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sequel-talk/1a7a6de6-3b65-429b-bbb2-4e4083bf9027%40googlegroups.com.

-- 
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 sequel-talk+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/E2A2E831-812C-4563-9D54-11259D685046%40gmail.com.


Re: Should Sequel continue shipping tests in the gem?

2020-01-19 Thread Adam Daniels
No strong feelings either way; when I run Sequel tests I'm normally 
authoring a patch, which means I have the git repository anyways. I have to 
wonder how frequently the tests in the gem are being run tho.

That said, I can imagine the tests in something with a C extension (such as 
sequel_pg) being more valuable shipping inside the gem, as that's more 
frequently where I seem to run into edge cases. C extensions with 
Linuxism's not building on FreeBSD. Not directly applicable to sequel 
itself obviously, but might affect things since sequel_pg piggybacks on 
sequel tests.

On Saturday, January 18, 2020 at 2:27:50 PM UTC-5, Jeremy Evans wrote:
>
> Sequel has always shipped tests in the gem, so that users and packagers 
> can test it without cloning the repository, but it does make the gem 
> significantly larger.  I'm open to feedback on whether Sequel should 
> continue to do this, so if you feel strongly one way or the other, please 
> respond.
>
> 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 sequel-talk+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/e3822936-097d-464b-86da-5c88f7bc0c01%40googlegroups.com.


Re: Aggregate Issues

2020-01-02 Thread Adam Daniels
Some Sequel dataset methods will return another dataset, and some are 
terminal (they'll execute the query immediately and return the result). In 
this case, the `count` method on a dataset is terminal and meant to be at 
the end of the line.

If you're looking for something more akin to SQL, then you can manually 
build your query and then extract out of it. It's probably not how I'd do 
it tho.

ds.select { count(id) }.where(kind: "yellow").single_value


On Thursday, January 2, 2020 at 11:13:40 AM UTC-5, BeeRich33 wrote:
>
> Hi folks.  I'm new to Sequel.  I'm having a hard time trying to count 
> qualified records in a working dataset, *ds*:
>
> puts ds.count(:id).where(kind: 'yellow').sql# => undefined method 
> `where' for 115:Integer (NoMethodError)
>
> Is my syntax wrong?  I can't generate sql for it as it throws an error.
>
> Is this improper syntax?  Any help appreciated.
>
> Cheers
>
>
>

-- 
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 sequel-talk+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/1b465f77-dd4d-4614-b9b3-dbbeb973e336%40googlegroups.com.


Re: How do I insert a JSON Array value into the column

2019-09-03 Thread Adam Daniels
You likely want to wrap the JSON values as well.

On Sequel 5.24:

DB.extension :pg_array, :pg_json

table.insert(
  payment_terms: Sequel.pg_array([
Sequel.pg_json_wrap('{"key1":"val1"}'),
Sequel.pg_json_wrap('{"key2":"val2"}'),
  ])
)



On Tue, Sep 3, 2019 at 2:52 AM Mohamed Najiullah <
mohammed.najiul...@gmail.com> wrote:

> I'm trying to store a JSON array into a column.
>
> I have declared the column like this
>
> column :payment_terms, "json[]"
>
> I'm able to see that the column creation has worked fine. But how do I
> insert a JSON array into it?
>
> I've tried
>
> table.insert(
> field_name: Sequel.pg_array([
>   '{"key1":"val1"}',
>  '{"key2":""val2}'
>])
>   )
>
> This doesn't seem to work.
>
> Could you please help me out?
>
> Thank You
>
> --
> 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 sequel-talk+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sequel-talk/9e5fb80d-8781-4c86-9688-2e82d53b388d%40googlegroups.com
> 
> .
>

-- 
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 sequel-talk+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/CAL9jp8kR4MvYMJTfDW3kH_BfZL1c0_%2BKC-aRjZLQbd%3Dck%3Dr-kw%40mail.gmail.com.


Re: String eager loading mode

2018-10-25 Thread Adam Daniels
I like the idea of this plugin and would likely use it if it was available.

I'm not sure how I feel about littering `require_eager_loading` around my
app, but since I don't have a better idea at this point I'm going to leave
it at that :)

On Thu, Oct 25, 2018 at 5:06 AM Janko Marohnić 
wrote:

> Hey everyone,
>
> In our application N+1 queries often creep in due to associations being
> loaded dynamically. Since they keep coming back, I would like to take it
> one step further and make Sequel raise an exception when an association was
> requested but hasn't been eager loaded.
>
> I know about the tactical_eager_loading
> 
> plugin, but I want to always make it explicit which associations are being
> loaded, I don't want it to be implicit.
>
> I was thinking that my "strict eager loading" plugin would behave like the
> following:
>
> There would be a dataset method, e.g. `#require_eager_loading`, which you
> can call before retrieving records. When that dataset loads model
> instances, attempting to call association retrieval methods on those
> instances will fail if the associations haven't been eager loaded (i.e. if
> Sequel::Model#associations cache doesn't contain the corresponding loaded
> association). The reasons for making this behaviour opt-in via a dataset
> method, as opposed to having it be global activated, is so that (a) you can
> gradually get your codebase rid of N+1s, and (b) you can still dynamically
> load associations in tests, as having to eager load associations in tests
> would probably add unnecessary verbosity.
>
> The model instances of associations eager loaded from a "strict eager
> loading" enabled dataset should also be required to eager load *their* 
> associations.
> In other words, the option that would be set by `#require_eager_loading`
> should "spread" onto the model instances of associations as well.
>
> The way we would would slowly migrate to "strict eager loading" mode in
> our app is that we would start making queries through our query objects (as
> opposed to through Sequel::Model or Sequel::Database). These query objects
> would then automatically add `#require_eager_loading` to each dataset. That
> way retrieving data through Sequel::Model or Sequel::Database would still
> allow dynamic loading of associations, but retrieving data through custom
> query objects wouldn't.
>
> What do you think about this idea? I was thinking that we would first
> write such a plugin internally in our app to try it out, and then I would
> probably propose it to Sequel. I think it would be a great asset to Sequel
> if it was able to disallow N+1 queries like that.
>
> Kind regards,
> Janko
>
> --
> 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 sequel-talk+unsubscr...@googlegroups.com.
> To post to this group, send email to sequel-talk@googlegroups.com.
> Visit this group at https://groups.google.com/group/sequel-talk.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 sequel-talk+unsubscr...@googlegroups.com.
To post to this group, send email to sequel-talk@googlegroups.com.
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.


Re: Efficiently declaring many to many associations filtered on the join model

2017-12-03 Thread Adam Daniels
On Saturday, December 2, 2017 at 11:10:06 PM UTC-5, Andrew Burleson wrote:
>
> I have an app where there are Writing Groups:
> - Each group can have many Users.
> - Users can have many books.
> - Users can submit a book to a group.
> - Books then proceed through several states, such as "pending review" -> 
> "in review" -> "accepted" | "rejected", etc.
>
> The Group <-> Books relation is very simple to set up using a table 
> `group_books` with the primary key (group_id, book_id).
>
> The basic associations are very easy to set up:
>
> class Group < Sequel::Model
>   one_to_many :group_books
>   many_to_many :books, join_table: :group_books
> end
>
> <3 Sequel!
>
> Now, using the group it would be helpful to filter books based on what 
> state they're in, eg. rather than show me all books, show me all books that 
> are pending review. In the database this means the group book's 
> `review_status` column has the value `pending_review.`
>
> It's possible to declare that association like so:
>
> many_to_many :books_pending_review, join_table: :group_books, class: :Book
> , right_key: :book_id do |ds|
>   ds.where(review_status: 'pending_review')
> end
>
> This works fine, but in practice I have some qualms with it:
>
> First, it duplicates knowledge. For instance, in the GroupBooks model I 
> would want to have the following:
>
> dataset_module do
>   def pending_review
> where(review_status: 'pending_review')
>   end
> end
>
> Now, I can chain that association if I want to return the group book 
> records, e.g.: `group.group_books.pending_review`, but that returns the 
> join models, not the book models.
>
> I'd prefer that the enum values that define book states in the database be 
> limited to the GroupBook class and be referenced via methods from the 
> outside world.
>
> A secondary, less important concern, is that this ends up being very 
> verbose if you have a large number of filtered associations in a full-size 
> app.
>
> So, is there a cleaner or better way to do this, ideally defining the 
> filtered associations in one place (GroupBook) and simply referencing them 
> (or chaining them) elsewhere?
>
> Thanks!
>
> - Andrew
>
>
>
 
I'm presuming you meant that Book would have review statuses, so you would 
define a dataset method on Book.

class Book < Sequel::Model
  dataset_module do
subset :pending_review, review_status: "pending_review"
  end
end


which you would then use as a condition in your many_to_many:

class Group < Sequel::Model
  one_to_many :book_groups
  many_to_many :books, join_table: :book_groups
  many_to_many :books_pending_review, join_table: :book_groups, class: 
:Book, right_key: :book_id do |ds|
ds.pending_review
  end
end


Unless you meant that books being submitted to groups would be reviewed 
individually for each submission? Your join table sounds like it doesn't 
have the review status, but if it does, that changes a bunch of things.

-- 
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 sequel-talk+unsubscr...@googlegroups.com.
To post to this group, send email to sequel-talk@googlegroups.com.
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.


Re: No luck with validation errors

2017-10-17 Thread Adam Daniels
How about something like this? Don't try to rescue exception, instead avoid 
it by checking if valid? before attempting save.

class Song < Sequel::Model
  plugin :validation_helpers

  def validate
super
validates_presence [:title, :length, :released_on, :lyrics]
  end
end

get '/new' do
  @song = Song.new
  slim :new_song
end

post '/' do
  @song = Song.new(params[:song])

  if @song.valid?
@song.save
flash[:notice] = "Song successfully added #{@song}"
redirect to("/#{@song}")
  else
flash[:notice] = @song.errors.full_messages.join("\n")
redirect to("/new")
  end
end



FYI, there's actually a better way than redirecting to "/new", and that is 
just rendering out the "new_song" template again if saving fails. That 
preserves error messages which you can then output beside each input (if 
you wanted to). Likely something to work towards since I believe you're 
following a book? and no point in complicating things too much.

-- 
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 sequel-talk+unsubscr...@googlegroups.com.
To post to this group, send email to sequel-talk@googlegroups.com.
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.


Re: ANDs wrapped in an OR wrapped in an AND again

2017-10-17 Thread Adam Daniels
On Tuesday, October 17, 2017 at 7:16:13 AM UTC-4, Beth Skurrie wrote:
>
> Imagine I have a table that has two pairs of coordinates, start_x, start_y 
> and end_x, end_y. I'm trying to find all lines that connect two points 1,1 
> and 2,2, without knowing which is the start and which is the end.
>
> This is almost there, but not quite.
>
> irb(main):004:0> DB[:lines].where(start_x: 1, start_y: 1).or(start_x: 2, 
> start_y: 2).where(end_x: 1, end_y: 1).or(end_x: 2, end_y: 2)
> => # = 1) AND (`start_y` = 1)) OR ((`start_x` = 2) AND (`start_y` = 2))) AND 
> (`end_x` = 1) AND (`end_y` = 1)) OR ((`end_x` = 2) AND (`end_y` = 2)))">
>
> The red part is where it goes wrong. I want it to do:
>
> SELECT * FROM `lines` WHERE (((`start_x` = 1) AND (`start_y` = 1)) OR 
> ((`start_x` 
> = 2) AND (`start_y` = 2))) AND (((`end_x` = 1) AND (`end_y` = 1)) OR 
> ((`end_x` 
> = 2) AND (`end_y` = 2)))
>
> I've trawled through all the querying documentation I can find, but 
> haven't been able to find an answer. Any help would be appreciated. 
>
> Thanks.
>

How about this?

DB[:lines].where { Sequel.|({start_x: 1, start_y: 1}, {start_x: 2, 
start_y: 2}) & Sequel.|({ end_x: 1, end_y: 1 }, { end_x: 2, end_y: 2 }) } 
# => "SELECT * FROM lines WHERE start_x = 1) AND (start_y = 1)) OR 
((start_x = 2) AND (start_y = 2))) AND (((end_x = 1) AND (end_y = 1)) OR 
((end_x = 2) AND (end_y = 2"


-- 
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 sequel-talk+unsubscr...@googlegroups.com.
To post to this group, send email to sequel-talk@googlegroups.com.
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.


Re: No luck with validation errors

2017-10-17 Thread Adam Daniels
On Monday, October 16, 2017 at 6:33:02 PM UTC-4, French Fry wrote:
>
> I've tried a number of things with no effect.  The call to validation 
> errors  is an  undefined method.  Can you please tell me what I am leaving 
> out? 
>
> And if it's okay to double up, since `def new` instantiates a new Song 
> object, I wanted to use `set` in `def create_song` and it would not work.  
> Right now, while it works (minus the validation error issues) it's 
> essentially creating a new Song object twice.  Originally I used insert, so 
> I replaced it with set, and then save, but no go.  Ok, thank you for the 
> help!
>
> class Song < Sequel::Model
>   plugin :validation_helpers
>   self.raise_on_save_failure
>
>   def validate
> super
> validates_presence [:title, :length, :released_on, :lyrics]
>   end
> end
>
> def create_song
> begin
>   @song = Song.create(params[:song])
> rescue
> flash[:notice] = @song.errors.full_messages
> end
>   end
> end
>
> get '/new' do
>   protected!
>   @song = Song.new
>   slim :new_song
> end
>
>
>
Just a few thoughts here - I cant' tell if you resolved this or not.

First - a method block is an implicit begin/end block, so if you are 
immediately inside a method definition, you can skip begin/end and just use 
rescue.

def create_song
  @song = Song.create(params[:song])
rescue Sequel::ValidationFailed
  flash[:notice] = @song.errors.full_messages
end

Second, raise_on_save_failures likely needs a true or false assignment to 
it, and I believe the default is true, so that line in your model is likely 
a no-op.

self.raise_on_save_failure = true

Third, we don't see where the create_song method is being called, so it's 
probably difficult for most of us to provide any valuable feedback in that 
regard. I'm assuming you have a post route that handles it.

-- 
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 sequel-talk+unsubscr...@googlegroups.com.
To post to this group, send email to sequel-talk@googlegroups.com.
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.


Re: Cannot load sequel/adapters

2017-08-28 Thread Adam Daniels
On Monday, August 28, 2017 at 1:45:49 PM UTC-4, dsch...@newrelic.com wrote:
>
> Sorry about that. It only occurred to me later that I should include the 
> code again.
>
> The current code is:
> require 'sinatra/sequel'
> require 'jdbc/mysql'
>
> DB = Sequel.connect('jdbc:mysql://dscheider@localhost/pelicans')
>
> migration "create teh pelicans table" do
>   DB.create_table :pelicans do
> primary_key :id
> text:name
> integer :age
>
> index [:name, :age], :unique => true
>   end
> end
>
>
> class Pelican < Sequel::Model
> end
>
> And the error I'm getting is:
> Sequel::AdapterNotFound: LoadError: no such file to load -- sqlite3
>   org/jruby/RubyKernel.java:961:in `require'
>   
> /Users/dscheider/.gem/jruby/2.3.3/gems/sequel-3.46.0/lib/sequel/adapters/sqlite.rb:1:in
>  
> `'
>   org/jruby/RubyKernel.java:961:in `require'
>   
> /Users/dscheider/.gem/jruby/2.3.3/gems/sequel-3.46.0/lib/sequel/core.rb:1:in 
> `block in (root)'
>   
> /Users/dscheider/.gem/jruby/2.3.3/gems/sequel-3.46.0/lib/sequel/core.rb:387:in
>  
> `block in tsk_require'
>   org/jruby/ext/thread/Mutex.java:148:in `synchronize'
>   
> /Users/dscheider/.gem/jruby/2.3.3/gems/sequel-3.46.0/lib/sequel/core.rb:100:in
>  
> `check_requiring_thread'
>   
> /Users/dscheider/.gem/jruby/2.3.3/gems/sequel-3.46.0/lib/sequel/core.rb:97:in 
> `check_requiring_thread'
>   
> /Users/dscheider/.gem/jruby/2.3.3/gems/sequel-3.46.0/lib/sequel/core.rb:387:in
>  
> `tsk_require'
>   
> /Users/dscheider/.gem/jruby/2.3.3/gems/sequel-3.46.0/lib/sequel/database/connecting.rb:25:in
>  
> `adapter_class'
>   
> /Users/dscheider/.gem/jruby/2.3.3/gems/sequel-3.46.0/lib/sequel/database/connecting.rb:55:in
>  
> `connect'
>   
> /Users/dscheider/.gem/jruby/2.3.3/gems/sequel-3.46.0/lib/sequel/core.rb:147:in
>  
> `connect'
>   
> /Users/dscheider/.gem/jruby/2.3.3/gems/sinatra-sequel-0.9.0/lib/sinatra/sequel.rb:21:in
>  
> `database'
>   
> /Users/dscheider/.gem/jruby/2.3.3/gems/sinatra-sequel-0.9.0/lib/sinatra/sequel.rb:46:in
>  
> `create_migrations_table'
>   
> /Users/dscheider/.gem/jruby/2.3.3/gems/sinatra-sequel-0.9.0/lib/sinatra/sequel.rb:34:in
>  
> `block in migration'
>   
> /Users/dscheider/.gem/jruby/2.3.3/gems/sinatra-1.4.2/lib/sinatra/base.rb:1915:in
>  
> `'
>   org/jruby/RubyKernel.java:961:in `require'
>   
> /Users/dscheider/repos/ruby_agent_playground/kevin_marx_1/database.rb:6:in 
> `(root)'
>   uri:classloader:/jruby/kernel/kernel.rb:1:in `'
>   org/jruby/RubyKernel.java:961:in `require'
>   uri:classloader:/jruby/kernel/kernel.rb:13:in `block in require_relative'
>   org/jruby/RubyBasicObject.java:1687:in `instance_eval'
>   /Users/dscheider/repos/ruby_agent_playground/kevin_marx_1/app.rb:3:in 
> `(root)'
>   /Users/dscheider/repos/ruby_agent_playground/kevin_marx_1/config.ru:1:in 
> `'
>   org/jruby/RubyKernel.java:1000:in `eval'
>   /Users/dscheider/repos/ruby_agent_playground/kevin_marx_1/config.ru:1:in 
> `new_from_string'
>   
> /Users/dscheider/.gem/jruby/2.3.3/gems/rack-1.5.2/lib/rack/builder.rb:55:in 
> `initialize'
>   /Users/dscheider/repos/ruby_agent_playground/kevin_marx_1/config.ru:1:in 
> `new_from_string'
>   
> /Users/dscheider/.gem/jruby/2.3.3/gems/rack-1.5.2/lib/rack/builder.rb:49:in 
> `new_from_string'
>   
> /Users/dscheider/.gem/jruby/2.3.3/gems/rack-1.5.2/lib/rack/builder.rb:40:in 
> `parse_file'
>   
> /Users/dscheider/.gem/jruby/2.3.3/gems/rack-1.5.2/lib/rack/server.rb:277:in 
> `build_app_and_options_from_config'
>   
> /Users/dscheider/.gem/jruby/2.3.3/gems/rack-1.5.2/lib/rack/server.rb:199:in 
> `app'
>   
> /Users/dscheider/.gem/jruby/2.3.3/gems/rack-1.5.2/lib/rack/server.rb:314:in 
> `'
>   org/jruby/RubyKernel.java:979:in `load'
>   
> /Users/dscheider/.gem/jruby/2.3.3/gems/rack-1.5.2/lib/rack/server.rb:250:in 
> `'
>   org/jruby/RubyKernel.java:979:in `load'
>   
> /Users/dscheider/.gem/jruby/2.3.3/gems/rack-1.5.2/lib/rack/server.rb:141:in 
> `start'
>   /Users/dscheider/.gem/jruby/2.3.3/gems/rack-1.5.2/bin/rackup:4:in 
> `(root)'
>   /Users/dscheider/.gem/jruby/2.3.3/bin/rackup:1:in `(root)'
>   /Users/dscheider/.gem/jruby/2.3.3/bin/rackup:22:in `(root)'
>
> So what I don't understand is why sqlite3 is being required in the first 
> place, since it isn't in my Gemfile or Gemfile.lock. It looks like it's 
> being required for the Sequel sqlite adapter, but that code must only be 
> running because of something I'm doing, since this presumably doesn't 
> happen every time a project doesn't use sqlite3.
>
>
You're encountering this line of code:

https://github.com/rtomayko/sinatra-sequel/blob/master/lib/sinatra/sequel.rb#L49

You can fix it by setting the DATABASE_URL environment variable to the same 
value you're using in your Sequel.connect method call.

This isn't a Sequel error/bug, as Sequel is doing exactly as instructed.

-- 
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, 

Re: automatically appending filter to model - at database level

2017-08-02 Thread Adam Daniels
On Wednesday, August 2, 2017 at 1:29:23 AM UTC-4, Roy Lam wrote:
>
> Hi,
>
> I am in the process of porting some code from active record to sequel. One 
> of them is called activerecord_perverted.rb :)
>
> What it does is to check if a table, in which a model is based on, 
> contains a specific column name, e.g. REPO_ID. If it does, append a where 
> clause automatically with something like REPO_ID = 'XYZ'. (XYZ is 
> determined dynamically depending on environment)
>
> Essentially, this behaviour is applied to most of my models (90%+). Only a 
> handful of table does not have the REPO_ID column.
>
> Is it possible to achieve the same in sequel?
>
> Many thanks
> Roy
>
> p.s. here is skeleton of the current code:
>
> module ActiveRecord
>  class Base
>   class << self
>def find_every(options)
> if columns_hash.include?("repo_id")
>  conditions = options[:conditions]
>  new_conditions = add_conditions(conditions) # append extra where 
> clause
>  options.update(:conditions => new_conditions)
> end
>end
>   end
>  end
> end
>


There was a solution posted for a very similar problem just recently - 
maybe it will work for you with some modification?

https://groups.google.com/d/msg/sequel-talk/O74jt4qBJuM/6xoBY4OvAgAJ

-- 
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 sequel-talk+unsubscr...@googlegroups.com.
To post to this group, send email to sequel-talk@googlegroups.com.
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.


Re: What should I change in my code in order to make this warning message disappear?

2017-07-21 Thread Adam Daniels
How about something like:

class Customer < Sequel::Model(ConnectionsHandler.crm[:customers])
end


> On Jul 21, 2017, at 12:16 PM, Pedro Vinícius  
> wrote:
> 
> Hey guys!
> 
> I'm working in a codebase whose purpose is integration a billing software 
> with a CRM one. As it's an integration between this two softwares, I have to 
> mantain two opened connections with two distinct databases at the same time. 
> In order to achieve that, I've created a connections handler class.
> 
> Some models must be connected to one database, and some models must be 
> connected to another. So, I'm making use of the "set_dataset" class method:
> 
> class Customer < Sequel::Model
>   set_dataset ConnectionsHandler.crm[:customers]
> end
> 
> class Message < Sequel::Model
>   set_dataset ConnectionsHandler.billing[:messages]
> end
> 
> But when I start my application, I'm seeing the following warning:
> 
> "SEQUEL DEPRECATION WARNING: Setting a model class dataset to an invalid 
> dataset is deprecated and will be removed in Sequel 5.  Either use a valid 
> dataset or set require_valid_table = false for the model class."
> 
> As told by Jeremy Evans at the issue #1376, this message is intended to help 
> with the code refactoring. But what do I have to change in order to make this 
> message disappear?
> 
> PS: When running this code with version 4.43.0, the message is not shown.
> 
> -- 
> 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 sequel-talk+unsubscr...@googlegroups.com 
> .
> To post to this group, send email to sequel-talk@googlegroups.com 
> .
> Visit this group at https://groups.google.com/group/sequel-talk 
> .
> For more options, visit https://groups.google.com/d/optout 
> .

-- 
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 sequel-talk+unsubscr...@googlegroups.com.
To post to this group, send email to sequel-talk@googlegroups.com.
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.


Re: many_to_many returning less records than expected

2017-07-08 Thread Adam Daniels
Apparently I didn't try all the option combinations. This got me what I 
needed.

many_to_many :fields, join_table: :templates, right_key: :id, 
right_primary_key: :template_id, left_primary_key: :template_id, left_key: 
:id

On Saturday, July 8, 2017 at 9:39:25 PM UTC-4, Adam Daniels wrote:
>
> I'm stuck on getting this many_to_many return the # of records I expect. 
> I've determined it's related to the type of JOIN, and the condition in the 
> WHERE.
>
> Could anybody give me a hand? I've gone through docs+multiple options and 
> not sure what I might have missed. The SQL I expect (and works manually) is 
> at bottom of gist in comment.
>
> https://gist.github.com/adam12/dd4620a927b882bdc445777110a8a081
>
> Thanks.
>

-- 
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 sequel-talk+unsubscr...@googlegroups.com.
To post to this group, send email to sequel-talk@googlegroups.com.
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.


many_to_many returning less records than expected

2017-07-08 Thread Adam Daniels
I'm stuck on getting this many_to_many return the # of records I expect. 
I've determined it's related to the type of JOIN, and the condition in the 
WHERE.

Could anybody give me a hand? I've gone through docs+multiple options and 
not sure what I might have missed. The SQL I expect (and works manually) is 
at bottom of gist in comment.

https://gist.github.com/adam12/dd4620a927b882bdc445777110a8a081

Thanks.

-- 
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 sequel-talk+unsubscr...@googlegroups.com.
To post to this group, send email to sequel-talk@googlegroups.com.
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.


Re: Eager load average of columns from other model

2017-06-27 Thread Adam Daniels
Once you upgrade Sequel, you can try this version. I forgot `avg` returns a 
value, where we actually need to `select` the value.

Item.select_append(Review.where(item_id: Sequel[:items][:id]).select { 
avg(:rating) }.as(:average_rating))

I used this to confirm it worked, in case you need it: 
https://gist.github.com/adam12/24935573836e5a53301707a834adf81c 
<https://gist.github.com/adam12/24935573836e5a53301707a834adf81c>


> On Jun 27, 2017, at 11:25 AM, Satyanarayana Gandham 
> <satyanarayana.gand...@careclues.com> wrote:
> 
> Hi Adam,
> 
> I am using sequel version 4.36.0 
> 
> In both the cases (using :items__id and Sequel.qualify(“items”, “id”)), I am 
> receiving " unknown column 'items.id' in 'where clause' ".
> 
> Same is the result when I use "Item.select_append { |p| Review.where(item_id: 
> p.items__id).avg(:rating) }" Ref: 
> http://sequel.jeremyevans.net/rdoc/files/doc/virtual_rows_rdoc.html#label-SQL-3A-3AQualifiedIdentifiers+-+Qualified+columns
>  
> <http://sequel.jeremyevans.net/rdoc/files/doc/virtual_rows_rdoc.html#label-SQL-3A-3AQualifiedIdentifiers+-+Qualified+columns>
> 
> Thanks,
> Satya
> 
> On Tuesday, June 27, 2017 at 8:42:15 PM UTC+5:30, Adam Daniels wrote:
> Likely my syntax (or your version of Sequel?), as I didn’t have a chance to 
> confirm.
> 
> You can try symbol splitting, or maybe Sequel.qualify
> 
> Item.select_append { Review.where(item_id: :items__id).avg(:rating) }
> 
> or
> 
> Item.select_append { Review.where(item_id: Sequel.qualify(“items”, 
> “id”)).avg(:rating) }
> 
> I noticed I passed a singular version of the table name originally so it 
> likely wouldn’t of worked anyways.
> 
> 
>> On Jun 27, 2017, at 11:09 AM, Satyanarayana Gandham <satyanaray...@ 
>> <>careclues.com <http://careclues.com/>> wrote:
>> 
>> Hi Adam,
>> 
>> Thanks for the reply. Unfortunately, I am getting an error. '[]' is not 
>> defined for Sequel:Module.
>> 
>> Regards,
>> Satya
>> 
>> On Monday, June 26, 2017 at 9:21:24 PM UTC+5:30, Adam Daniels wrote:
>> I believe you can use `select_append` with a subquery to return a column 
>> representing the average rating.
>> 
>> Something like:
>> 
>> Item.select_append { Review.where(item_id: 
>> Sequel[:item][:id]).avg(:rating) }
>> 
>> You’ll have to access this field using the Array-style accessor, as it won’t 
>> exist as a method on the Item instances.
>> 
>>> On Jun 26, 2017, at 10:41 AM, Satyanarayana Gandham 
>>> <satyanaray...@careclues.com <>> wrote:
>>> 
>>> Hi,
>>> 
>>> I have two models Item and Review. Item has one_to_many association with 
>>> Review. Review has rating column which stores integers from 1 to 10. Rating 
>>> of an item equals avg of the rating column in item's reviews_dataset.
>>> 
>>> When I list the items, I want to list the rating as well. But the issues 
>>> here is that I am calling DB 'N' no. of times for each item. Basically a N 
>>> + 1 problem. I have used eager loading for the associations to solve the N 
>>> +1 issue for the associations. But, the average here is not an association. 
>>> Is there a way I can use eager functionality (or some other way) to load 
>>> the average of a column in the associated dataset and not face the N +1 
>>> issue.
>>> 
>>> Thanks,
>>> Satya
>>> 
>>> -- 
>>> 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 sequel-talk...@ <>googlegroups. <http://googlegroups.com/>com 
>>> <http://googlegroups.com/>.
>>> To post to this group, send email to seque...@ <>googlegroups.com 
>>> <http://googlegroups.com/>.
>>> Visit this group at https://groups.google.com/group/sequel-talk 
>>> <https://groups.google.com/group/sequel-talk>.
>>> For more options, visit https://groups.google.com/d/optout 
>>> <https://groups.google.com/d/optout>.
>> 
>> 
>> -- 
>> 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 sequel-talk...@googlegroups.com <>.
>> To post to this group, send email to seque...@googlegroups.com <>.
>> Visit this group at https://groups.google.com/group/sequel-talk

Re: Eager load average of columns from other model

2017-06-27 Thread Adam Daniels
Likely my syntax (or your version of Sequel?), as I didn’t have a chance to 
confirm.

You can try symbol splitting, or maybe Sequel.qualify

Item.select_append { Review.where(item_id: :items__id).avg(:rating) }

or

Item.select_append { Review.where(item_id: Sequel.qualify(“items”, 
“id”)).avg(:rating) }

I noticed I passed a singular version of the table name originally so it likely 
wouldn’t of worked anyways.


> On Jun 27, 2017, at 11:09 AM, Satyanarayana Gandham 
> <satyanarayana.gand...@careclues.com> wrote:
> 
> Hi Adam,
> 
> Thanks for the reply. Unfortunately, I am getting an error. '[]' is not 
> defined for Sequel:Module.
> 
> Regards,
> Satya
> 
> On Monday, June 26, 2017 at 9:21:24 PM UTC+5:30, Adam Daniels wrote:
> I believe you can use `select_append` with a subquery to return a column 
> representing the average rating.
> 
> Something like:
> 
> Item.select_append { Review.where(item_id: 
> Sequel[:item][:id]).avg(:rating) }
> 
> You’ll have to access this field using the Array-style accessor, as it won’t 
> exist as a method on the Item instances.
> 
>> On Jun 26, 2017, at 10:41 AM, Satyanarayana Gandham 
>> <satyanaray...@careclues.com > wrote:
>> 
>> Hi,
>> 
>> I have two models Item and Review. Item has one_to_many association with 
>> Review. Review has rating column which stores integers from 1 to 10. Rating 
>> of an item equals avg of the rating column in item's reviews_dataset.
>> 
>> When I list the items, I want to list the rating as well. But the issues 
>> here is that I am calling DB 'N' no. of times for each item. Basically a N + 
>> 1 problem. I have used eager loading for the associations to solve the N +1 
>> issue for the associations. But, the average here is not an association. Is 
>> there a way I can use eager functionality (or some other way) to load the 
>> average of a column in the associated dataset and not face the N +1 issue.
>> 
>> Thanks,
>> Satya
>> 
>> -- 
>> 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 sequel-talk...@googlegroups.com .
>> To post to this group, send email to seque...@googlegroups.com .
>> Visit this group at https://groups.google.com/group/sequel-talk 
>> <https://groups.google.com/group/sequel-talk>.
>> For more options, visit https://groups.google.com/d/optout 
>> <https://groups.google.com/d/optout>.
> 
> 
> -- 
> 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 sequel-talk+unsubscr...@googlegroups.com 
> <mailto:sequel-talk+unsubscr...@googlegroups.com>.
> To post to this group, send email to sequel-talk@googlegroups.com 
> <mailto:sequel-talk@googlegroups.com>.
> Visit this group at https://groups.google.com/group/sequel-talk 
> <https://groups.google.com/group/sequel-talk>.
> For more options, visit https://groups.google.com/d/optout 
> <https://groups.google.com/d/optout>.

-- 
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 sequel-talk+unsubscr...@googlegroups.com.
To post to this group, send email to sequel-talk@googlegroups.com.
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.


Re: Eager load average of columns from other model

2017-06-26 Thread Adam Daniels
I believe you can use `select_append` with a subquery to return a column 
representing the average rating.

Something like:

Item.select_append { Review.where(item_id: Sequel[:item][:id]).avg(:rating) 
}

You’ll have to access this field using the Array-style accessor, as it won’t 
exist as a method on the Item instances.

> On Jun 26, 2017, at 10:41 AM, Satyanarayana Gandham 
>  wrote:
> 
> Hi,
> 
> I have two models Item and Review. Item has one_to_many association with 
> Review. Review has rating column which stores integers from 1 to 10. Rating 
> of an item equals avg of the rating column in item's reviews_dataset.
> 
> When I list the items, I want to list the rating as well. But the issues here 
> is that I am calling DB 'N' no. of times for each item. Basically a N + 1 
> problem. I have used eager loading for the associations to solve the N +1 
> issue for the associations. But, the average here is not an association. Is 
> there a way I can use eager functionality (or some other way) to load the 
> average of a column in the associated dataset and not face the N +1 issue.
> 
> Thanks,
> Satya
> 
> -- 
> 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 sequel-talk+unsubscr...@googlegroups.com 
> .
> To post to this group, send email to sequel-talk@googlegroups.com 
> .
> Visit this group at https://groups.google.com/group/sequel-talk 
> .
> For more options, visit https://groups.google.com/d/optout 
> .

-- 
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 sequel-talk+unsubscr...@googlegroups.com.
To post to this group, send email to sequel-talk@googlegroups.com.
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.


Re: anybody using mutations gem?

2017-04-29 Thread Adam Daniels
I'm not using the mutations gem, but I created two libraries which kind of 
do the same thing, albeit separately.

The first is form validations - I use Forme, and I wanted to be able to 
pass objects to represent the form state and validation cycle. It copies 
the same validation structure as Sequel (and most of the validations are 
almost 1-to-1 copies of the validation implementation).

If my form object validates, I may call a command object to complete a 
complex action, keeping my routes (I use Roda) very request/response 
specific. This library is paired with the Wisper gem to give me a very nice 
callback-style syntax.

Here's a bit of the code in action (a Roda route). The form is obviously 
Machine::CreateForm, and the Sequel interactions happen inside the 
CreateMachine command.

  r.post true do
check_machine_count_limit(rodauth.current_account_group)

@schema = Machine::CreateForm.new(r.params["form"])

r.halt view("machines/new") if @schema.invalid?

CreateMachine.call(@schema, account_group: 
rodauth.current_account_group) do
  on(:ok) do |machine, password|
flash[:password] = password if password
r.redirect machine_path(machine)
  end

  on(:no_compute_node) do
Mailer.error_email("No compute node available")
r.halt 422, view("machines/new", locals: { error: "No compute 
nodes available" })
  end

  on(:no_ip_address) do
Mailer.error_email("No IP Address available")
r.halt 422, view("machines/new", locals: { error: "No IP 
address available" })
  end

  on(:exception) do |e|
Mailer.error_email(e)
message = App.production? ? "An error occured" : e.message

r.halt 422, view("machines/new", locals: { error: message })
  end
end
  end

I might extract these two libraries from my project and release them as 
gems if they gain a bit more maturity, but so far, I like the composition 
of them when completing complex actions crossing multiple models with many 
different failures. I don't use them for the normal CRUD style actions.

On Saturday, April 29, 2017 at 9:53:24 PM UTC-4, dota? =op wrote:
>
> i just found this gem: 
>
> https://github.com/cypriss/mutations 
>
> and wondered if someone is using it with sequel. 
>
> how do you validate your models? do you keep all validation logic 
> inside teh Command? 
>
> any tips on coupling with sequel? 
>
> -- 
> Igor 
>

-- 
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 sequel-talk+unsubscr...@googlegroups.com.
To post to this group, send email to sequel-talk@googlegroups.com.
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.


Re: Limited association with reversed :order

2017-04-19 Thread Adam Daniels
I would keep the one_to_many :subscriptions association and then define the 
one_to_one as you did, but define an `active_subscription` one_to_one.

one_to_one :active_subscription, class: 'DonorSee::Subscription', 
conditions: { active: true }

Untested but I'm assuming it will work.

On Wednesday, April 19, 2017 at 1:46:02 PM UTC-4, Barbara Carradini wrote:
>
> I have an alternate solution that I believe is elegant enough. I am going 
> to define the standard one_to_many association between User and 
> Subscription:
>
>   class User < Sequel::Model
> one_to_many :subscriptions
>   end
>
> Then simply define a method for the User model that gets me what I want:
>
> def subscription
>   subscriptions_dataset.first(active: true)
> end
>
> Thanks anyway!
>
>
> On Wednesday, April 19, 2017 at 1:33:04 PM UTC-4, Barbara Carradini wrote:
>>
>> I have a User model and a Subscription model. The Subscription model 
>> contains, among other things, a foreign key that points back to User 
>> (user_id) and a Boolean (active) that specifies whether the Subscription is 
>> active or inactive. There can only be one active Subscription for a User at 
>> any time, though there may be multiple canceled/inactive Subscriptions for 
>> that user. I'd like to specify an association for the User model that grabs 
>> the first active, associated Subscription (which, because of other measures 
>> I've taken will be the ONLY active, associated Subscription). I can almost 
>> get there with the following in the User model:
>>
>> one_to_one :subscription, :class=>'DonorSee::Subscription', 
>> :order=>:active, :limit=>1
>>
>> However, this returns the first *inactive* Subscription, since false 
>> orders before true. I want the first *active* Subscription. I've tried 
>> the following:
>>
>> one_to_one :subscription, :class=>'DonorSee::Subscription', 
>> :reverse_order=>:active, :limit=>1
>>
>> but I still get the first inactive Subscription when I invoke 
>> user.subscription:
>>
>>
>> Here are my models (simplified):
>>
>>   class Subscription < Sequel::Model
>> many_to_one :user
>>   end
>>
>>   class User < Sequel::Model
>> one_to_one :subscription, class: 'DonorSee::Subscription', 
>> :reverse_order=>:active, :limit=>1
>>   end
>>
>>

-- 
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 sequel-talk+unsubscr...@googlegroups.com.
To post to this group, send email to sequel-talk@googlegroups.com.
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.


Re: Aggregate attributes and eager loading

2017-04-18 Thread Adam Daniels
You're looking for a counter cache. I don't believe there is anything built 
in to Sequel, but I'm pretty sure you have two options off the top of my 
head.

1. Use after_add/after_remove callbacks on the `one_to_many` association, 
which count the associated `stuffs` and then update the counter cache. 

http://sequel.jeremyevans.net/rdoc/files/doc/association_basics_rdoc.html#label-3Aafter_add+-5Bone_to_many-2C+many_to_many-5D

2. Use Jeremy's sequel_postgresql_triggers gem if you're running postgres.


On Tuesday, April 18, 2017 at 6:40:01 PM UTC-4, dota? =op wrote:
>
> EHLO, 
>
> i have those Account and Stuff models. Account.one_to_many(:stuffs) 
> and i want it to have Account#stuff_count attribute so i can display 
> it on my views. And i also want it to be eager loaded, so i dont kill 
> my db. 
>
> Hints on how this can be achieved? 
>
> <3 
>
> -- 
> Igor 
>

-- 
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 sequel-talk+unsubscr...@googlegroups.com.
To post to this group, send email to sequel-talk@googlegroups.com.
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.