jackdempsey / sequel_polymorphic Goto Github PK
View Code? Open in Web Editor NEWLicense: MIT License
License: MIT License
class App::Post < Sequel::Model
many_to_many :tags, :through => :taggings, :as => :taggable
end
class App::Tagging < Sequel::Model
###
the code above will fail with error uninitialized constant Tagging
because of this line
After installing the sequel_polymorphic gem, I could not figure out how to run the plugin under sequel 3.5.0. After trying out Sequel::Model.plugin() to load the plugin, I figured out that the 'is' method is from sequel 2.0.
Does the plugin only work with Sequel 2.0, or if not, what is the syntax for loading the plugin in more recent versions?
Also, showing the necessary require's and setup for your example code in the readme would have been really helpful. I had no idea how to load the plugin from your documentation.
Thanks,
-Sam
Sequel 5 just got released and it removed some stuff and changed stuff that polymorphic required, such as the plugin :schema.
But also the assoc. model library got changed so it doesn't work.
/home/jnylen/Projekt/sequel_polymorphic/vendor/bundle/ruby/2.4.0/gems/sequel-5.0.0/lib/sequel/dataset/query.rb:84: warning: statement not reached
/home/jnylen/Projekt/sequel_polymorphic/vendor/bundle/ruby/2.4.0/gems/sequel-5.0.0/lib/sequel/model/associations.rb:2546: warning: assigned but unused variable - res
/home/jnylen/Projekt/sequel_polymorphic/lib/sequel/plugins/polymorphic.rb:61: warning: shadowing outer local variable - model
/home/jnylen/Projekt/sequel_polymorphic/lib/sequel/plugins/polymorphic.rb:67: warning: shadowing outer local variable - id_map
/home/jnylen/Projekt/sequel_polymorphic/lib/sequel/plugins/polymorphic.rb:70: warning: shadowing outer local variable - model
/home/jnylen/Projekt/sequel_polymorphic/vendor/bundle/ruby/2.4.0/gems/sequel-5.0.0/lib/sequel/model/base.rb:922: warning: instance variable @overridable_methods_module not initialized
/home/jnylen/Projekt/sequel_polymorphic/vendor/bundle/ruby/2.4.0/gems/sequel-5.0.0/lib/sequel/model/base.rb:922: warning: instance variable @overridable_methods_module not initialized
/home/jnylen/Projekt/sequel_polymorphic/vendor/bundle/ruby/2.4.0/gems/sequel-5.0.0/lib/sequel/model/base.rb:922: warning: instance variable @overridable_methods_module not initialized
/home/jnylen/Projekt/sequel_polymorphic/vendor/bundle/ruby/2.4.0/gems/sequel-5.0.0/lib/sequel/model/base.rb:922: warning: instance variable @overridable_methods_module not initialized
/home/jnylen/Projekt/sequel_polymorphic/vendor/bundle/ruby/2.4.0/gems/sequel-5.0.0/lib/sequel/model/base.rb:922: warning: instance variable @overridable_methods_module not initialized
/home/jnylen/Projekt/sequel_polymorphic/vendor/bundle/ruby/2.4.0/gems/sequel-5.0.0/lib/sequel/model/base.rb:922: warning: instance variable @overridable_methods_module not initialized
/home/jnylen/Projekt/sequel_polymorphic/vendor/bundle/ruby/2.4.0/gems/sequel-5.0.0/lib/sequel/model/base.rb:922: warning: instance variable @overridable_methods_module not initialized
/home/jnylen/Projekt/sequel_polymorphic/vendor/bundle/ruby/2.4.0/gems/sequel-5.0.0/lib/sequel/model/base.rb:922: warning: instance variable @overridable_methods_module not initialized
Coverage report generated for Mintest to /home/jnylen/Projekt/sequel_polymorphic/coverage. 52 / 92 LOC (56.52%) covered.
/home/jnylen/Projekt/sequel_polymorphic/vendor/bundle/ruby/2.4.0/gems/sequel-5.0.0/lib/sequel/model/associations.rb:1906:in `define_method': nil is not a symbol nor a string (TypeError)
from /home/jnylen/Projekt/sequel_polymorphic/vendor/bundle/ruby/2.4.0/gems/sequel-5.0.0/lib/sequel/model/associations.rb:1906:in `block in association_module_def'
from /home/jnylen/Projekt/sequel_polymorphic/vendor/bundle/ruby/2.4.0/gems/sequel-5.0.0/lib/sequel/model/associations.rb:1906:in `module_eval'
from /home/jnylen/Projekt/sequel_polymorphic/vendor/bundle/ruby/2.4.0/gems/sequel-5.0.0/lib/sequel/model/associations.rb:1906:in `association_module_def'
from /home/jnylen/Projekt/sequel_polymorphic/vendor/bundle/ruby/2.4.0/gems/sequel-5.0.0/lib/sequel/model/associations.rb:1911:in `association_module_private_def'
from /home/jnylen/Projekt/sequel_polymorphic/vendor/bundle/ruby/2.4.0/gems/sequel-5.0.0/lib/sequel/model/associations.rb:1952:in `def_association_instance_methods'
from /home/jnylen/Projekt/sequel_polymorphic/vendor/bundle/ruby/2.4.0/gems/sequel-5.0.0/lib/sequel/model/associations.rb:1822:in `associate'
from /home/jnylen/Projekt/sequel_polymorphic/lib/sequel/plugins/polymorphic.rb:167:in `one_to_one'
from /home/jnylen/Projekt/sequel_polymorphic/spec/sequel_setup.rb:75:in `<class:Question>'
from /home/jnylen/Projekt/sequel_polymorphic/spec/sequel_setup.rb:74:in `<top (required)>'
from /home/jnylen/Projekt/sequel_polymorphic/spec/spec_helper.rb:13:in `require'
from /home/jnylen/Projekt/sequel_polymorphic/spec/spec_helper.rb:13:in `<top (required)>'
from /home/jnylen/Projekt/sequel_polymorphic/spec/sequel_polymorphic/many_to_many_association_spec.rb:1:in `require'
from /home/jnylen/Projekt/sequel_polymorphic/spec/sequel_polymorphic/many_to_many_association_spec.rb:1:in `<top (required)>'
from /home/jnylen/Projekt/sequel_polymorphic/vendor/bundle/ruby/2.4.0/gems/rake-11.3.0/lib/rake/rake_test_loader.rb:15:in `require'
from /home/jnylen/Projekt/sequel_polymorphic/vendor/bundle/ruby/2.4.0/gems/rake-11.3.0/lib/rake/rake_test_loader.rb:15:in `block in <main>'
from /home/jnylen/Projekt/sequel_polymorphic/vendor/bundle/ruby/2.4.0/gems/rake-11.3.0/lib/rake/rake_test_loader.rb:4:in `select'
from /home/jnylen/Projekt/sequel_polymorphic/vendor/bundle/ruby/2.4.0/gems/rake-11.3.0/lib/rake/rake_test_loader.rb:4:in `<main>'
rake aborted!
The latest commit (e35f528) breaks my associations (although it fixed my issue with 'constantize', which is good); I have a column type that is camel-cased (i.e., clinical_type: "ClinicalColorectal"). Calling capitalize on this value results in incorrect behavior, and the association fails because it cannot find a class named "Clinicalcolorectal". I'm not sure why it should be necessary to transform able_type when loading - surely this column should be written with the exact class name?
Hi @kuraga and @jackdempsey,
Is it true you can't run, i.e.:
Episode.where(media: Project.first)
As it returns:
NameError: uninitialized constant Media (this happened when attempting to find the associated class for #<Sequel::Model::Associations::ManyToOneAssociationReflection Episode.many_to_one :media, :reciprocal=>:episodes, :reciprocal_type=>:many_to_one, :setter=>#<Proc:0x00000004dae1a8@/home/vagrant/.rvm/gems/ruby-2.3.3/bundler/gems/sequel_polymorphic-ea47ba8eef73/lib/sequel/plugins/polymorphic.rb:28>, :dataset=>#<Proc:0x00000004dae180@/home/vagrant/.rvm/gems/ruby-2.3.3/bundler/gems/sequel_polymorphic-ea47ba8eef73/lib/sequel/plugins/polymorphic.rb:32>, :eager_loader=>#<Proc:0x00000004dae158@/home/vagrant/.rvm/gems/ruby-2.3.3/bundler/gems/sequel_polymorphic-ea47ba8eef73/lib/sequel/plugins/polymorphic.rb:39>, :polymorphic=>true>)
from /home/vagrant/.rvm/gems/ruby-2.3.3/gems/activesupport-5.1.3/lib/active_support/inflector/methods.rb:269:in `const_get'
Do you have any idea to inject this into .where rather than creating a .polymorphic_where function?
Good day! We have
:dataset => (proc do
klass = constantize(send(:"#{able}_type"))
klass.where(klass.primary_key => send(:"#{able}_id"))
end)
But an error will be thrown if association is absent (send(:"#{able}_type").nil? && send(:"#{able}_id").nil?
).
In :eager_loader
option's proc, similar.
Thanks!
/cc @jackdempsey @contentfree
hi!
I have:
class MyGem::User < Sequel::Model
# plugin :nested_attributes
# plugin :association_dependencies
one_to_many :tasks, as: :taskable
# nested_attributes :tasks
# add_association_dependencies tasks: :destroy
end
# db columns: ... taskable_type(String), taskable_id(Integer)
class MyGem::Task < Sequel::Model
many_to_one :taskable, polymorphic: true
end
If i try from console:
$ @user = User.first
$ @task = Task.first
$ @task.taskable = @user
$ @task.save
i receive this:
Sequel::DatabaseError: PG::UndefinedColumn: ERROR: column "taskable" of relation "tasks" does not exist
LINE 3: ...ble_id" = 3, "taskable_type" = 'User', "taskable"...
rails-5.1.3
sequel-4.46.0
sequel_polymorphic-0.3.1
and i am stuck :(
any thoughts how it can be fixed or, at least, how can i investigate it more deeply - why? thanks
If the foreign_key is not an integer the query generated won't be valid:
Sequel::DatabaseError:
PG::UndefinedFunction: ERROR: operator doesn't exist: text = integer
LINE 1: ... 'User') AND ("user_comments"."object_id" = 14011)) ^
Pretty straightforward: The example in the readme uses is :polymorphic
and that's no longer valid. Bit me (as a newb to Sequel).
is it possible to override the default prefix_type value? We are building a gem which includes different models of our main app, but those models are under a new namespace.
This is the problem we are having:
module GemNamespace
class Preference < Sequel::Model
many_to_one :configurable, polymorphic: true
end
end
module GemNamespace
class Account < Sequel::Model
one_to_many :preferences, as: :configurable, type: 'Account'
end
end
The default value of prefix_type is equal to "GemNamespace::Account" and I want just "Account".
does it make sense?
In your README example, if you try Asset.first.attachable
then you will get uninitialized constant Attachable
. For some reason it isn't realizing that it should be fetching a Note
instead of an Attachable
.
Works fine if you start with the Note
and try to do a round-trip, i.e. note.assets.first.attachable
.
P.S. Now I see that our project isn't so brilliant... It violates Sequel way...
Just for instance. See here:
collection_singular = singularize(collection_name.to_s).to_sym
collection_singular_id = :"#{collection_singular}_id"
through_klass = constantize(singularize(camelize(through.to_s)))
<...>
:adder => proc do |many_of_instance|
through_klass.create(collection_singular_id => many_of_instance.pk, able_id => pk, able_type => self.class.to_s)
end
And in Sequel:
lcks = opts[:left_keys] = Array(left)
lcpks = opts[:left_primary_keys] = Array(left_pk)
def _join_table_dataset(opts)
ds = model.db.from(opts.join_table_source)
opts[:join_table_block] ? opts[:join_table_block].call(ds) : ds
end
<...>
opts[:adder] ||= proc do |o|
h = {}
lcks.zip(lcpks).each{|k, pk| h[k] = get_column_value(pk)}
rcks.zip(opts.right_primary_key_methods).each{|k, pk| h[k] = o.get_column_value(pk)}
_join_table_dataset(opts).insert(h)
end
Just feel the difference and incompatibilities...
Good day, @jackdempsey !
Can you add this repository to Travis and turn on hooks for it, please? I'm adding a config after.
Thanks!
Hi there, I attempted to use this plugin and when I attempted to load a polymorphic association I get this error:
undefined method `constantize' for #<StorageItem:0x0000000175e080>
Which comes to this line: https://github.com/jackdempsey/sequel_polymorphic/blob/master/lib/sequel_polymorphic/sequel_polymorphic.rb#L34
I'm not sure if the method is unavailable inside the proc or if I need to configure some other extension on my Sequel.
For a quick fix I just added the constantize(string)
method to the Sequel::Model
class. Looking at the Sequel's source code, I found a constantize
private instance method on the model's Inflection
module, but my Sequel::Model
does not seem to have it included. Any clues on how that could happen?
Here are the included modules on my Sequel::Model
2.1.5 :002 > Sequel::Model.included_modules
=> [Sequel::Plugins::Polymorphic::InstanceMethods, Sequel::Plugins::ActiveModel::InstanceMethods,
Sequel::Plugins::Timestamps::InstanceMethods, Sequel::Model::Associations::InstanceMethods,
Sequel::Model::InstanceMethods, Kernel]
And my initialization:
require 'sequel'
Sequel::Model.plugin :timestamps
Sequel::Model.plugin :active_model
Sequel::Model.plugin :polymorphic
@@db = ::Sequel.connect(config.database)
@@db.extension(:pagination)
The versions are: Sequel 4.24.0 and Sequel Polymorphic v0.2
Thanks for any help!
I was told to bring this over here (jeremyevans/sequel#1617)...
I have an Account
with an Address
. Because Address
is also used for other things, I want it to have a polymorphic association. Unfortunately, this means that nested attribute setting isn't working correctly. Namely, it isn't persisting the nested object. It gets created alright, and the parent object gets saved, but not the child.
In the code below, when I got rid of the polymorphism (changing Address::record
to Address::account
), everything worked as expected.
require 'sequel'
# require 'logger'
DB = Sequel.sqlite
# DB.loggers = [Logger.new(STDOUT)]
DB.create_table? :accounts do
primary_key :id
end
DB.create_table? :addresses do
primary_key :id
column :record_id, "bigint"
column :record_type, "character varying"
column :street, "text"
end
class Account < Sequel::Model
plugin :polymorphic
one_to_one :address, as: :record
plugin :nested_attributes
nested_attributes :address, destroy: true
end
class Address < Sequel::Model
plugin :polymorphic
many_to_one :record, polymorphic: true
end
puts "database is set..."
account = Account.create(address_attributes: { street: "123 Sesame Street" })
puts account.inspect
puts account.address.inspect
puts "Account count: #{Account.count}"
puts "Address count: #{Address.count}"
DB.drop_table :accounts, :addresses
outputs...
database is set...
#<Account @values={:id=>1, :record_id=>nil, :record_type=>"Address"}>
#<Address @values={:street=>"123 Sesame Street"}>
Account count: 1
Address count: 0
I expected Address count
to be 1, and the Address
to have an id set.
N/A
With logging output to console, the above script outputs the following:
I, [2019-05-02T13:48:36.421868 #21143] INFO -- : (0.000092s) SELECT sqlite_version()
I, [2019-05-02T13:48:36.422390 #21143] INFO -- : (0.000247s) CREATE TABLE IF NOT EXISTS `accounts` (`id` integer NOT NULL PRIMARY KEY AUTOINCREMENT)
I, [2019-05-02T13:48:36.422681 #21143] INFO -- : (0.000068s) CREATE TABLE IF NOT EXISTS `addresses` (`id` integer NOT NULL PRIMARY KEY AUTOINCREMENT, `record_id` bigint, `record_type` character varying, `street` text)
I, [2019-05-02T13:48:36.423029 #21143] INFO -- : (0.000072s) PRAGMA table_info('accounts')
I, [2019-05-02T13:48:36.436611 #21143] INFO -- : (0.000169s) PRAGMA table_info('addresses')
database is set...
I, [2019-05-02T13:48:36.437642 #21143] INFO -- : (0.000033s) BEGIN
I, [2019-05-02T13:48:36.437808 #21143] INFO -- : (0.000043s) INSERT INTO `accounts` DEFAULT VALUES
I, [2019-05-02T13:48:36.437994 #21143] INFO -- : (0.000067s) SELECT * FROM `accounts` WHERE `id` = 1
I, [2019-05-02T13:48:36.438106 #21143] INFO -- : (0.000018s) COMMIT
#<Account @values={:id=>1, :record_id=>nil, :record_type=>"Address"}>
#<Address @values={:street=>"123 Sesame Street"}>
I, [2019-05-02T13:48:36.438375 #21143] INFO -- : (0.000044s) SELECT count(*) AS 'count' FROM `accounts` LIMIT 1
Account count: 1
I, [2019-05-02T13:48:36.438550 #21143] INFO -- : (0.000039s) SELECT count(*) AS 'count' FROM `addresses` LIMIT 1
Address count: 0
I, [2019-05-02T13:48:36.438694 #21143] INFO -- : (0.000084s) DROP TABLE `accounts`
I, [2019-05-02T13:48:36.438806 #21143] INFO -- : (0.000061s) DROP TABLE `addresses`
@jackdempsey, @graft, @Draiken, @nazipov, help me, please!
What do you think about eager loading support of this gem?
Is it correct?
Is it full?
Why do we have eager_loader
option for many-to-one association only?
Thanks!
class Status < Sequel::Model
one_to_many :likes, as: :likeable
end
class Like < Sequel::Model
many_to_one :likeable, polymorphic: true
end
Like.eager(:likeable).all
vendor/bundle/ruby/2.2.0/gems/sequel-4.24.0/lib/sequel/model/inflections.rb:124:in `constantize': "status" is not a valid constant name! (NameError)
from vendor/bundle/ruby/2.2.0/gems/sequel_polymorphic-0.2.0/lib/sequel_polymorphic/sequel_polymorphic.rb:48:in `block (2 levels) in many_to_one'
from vendor/bundle/ruby/2.2.0/gems/sequel_polymorphic-0.2.0/lib/sequel_polymorphic/sequel_polymorphic.rb:46:in `each'
from vendor/bundle/ruby/2.2.0/gems/sequel_polymorphic-0.2.0/lib/sequel_polymorphic/sequel_polymorphic.rb:46:in `block in many_to_one'
from vendor/bundle/ruby/2.2.0/gems/sequel-4.24.0/lib/sequel/model/associations.rb:2833:in `call'
from vendor/bundle/ruby/2.2.0/gems/sequel-4.24.0/lib/sequel/model/associations.rb:2833:in `block in eager_load'
from vendor/bundle/ruby/2.2.0/gems/sequel-4.24.0/lib/sequel/model/associations.rb:2797:in `each'
from vendor/bundle/ruby/2.2.0/gems/sequel-4.24.0/lib/sequel/model/associations.rb:2797:in `eager_load'
from vendor/bundle/ruby/2.2.0/gems/sequel-4.24.0/lib/sequel/model/associations.rb:2894:in `post_load'
from vendor/bundle/ruby/2.2.0/gems/sequel-4.24.0/lib/sequel/dataset/actions.rb:862:in `_all'
from vendor/bundle/ruby/2.2.0/gems/sequel-4.24.0/lib/sequel/dataset/actions.rb:45:in `all'
from app.rb:12:in `<main>'
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.