Replies: 2 comments
-
Thanks for the report. Looks like this example code was written before multiple reciprocal types were supported. It gives an exception trying to determine the reciprocal type, because the default behavior for that is to look at the associations of the associated class, and there is no associated class in this case. Changing: many_to_one :author, reciprocal: :comments, to: many_to_one :author, reciprocal: :comments, reciprocal_type: :one_to_many, Fixes things. I'll update the documentation. Here's a more complete example testing that all parts work: DB.create_table(:comments) do
primary_key :id
Integer :author_id
String :author_type
end
DB.create_table(:users) do
primary_key :id
end
DB.create_table(:admins) do
primary_key :id
end
Sequel.extension :inflector
class Comment < Sequel::Model
many_to_one :author, reciprocal: :comments, reciprocal_type: :one_to_many,
setter: (lambda do |author|
self[:author_id] = (author.pk if author)
self[:author_type] = (author.class.name if author)
end),
dataset: (proc do
klass = author_type.constantize
klass.where(klass.primary_key=>author_id)
end),
eager_loader: (lambda do |eo|
id_map = {}
eo[:rows].each do |comment|
comment.associations[:author] = nil
((id_map[comment.author_type] ||= {})[comment.author_id] ||= []) << comment
end
id_map.each do |klass_name, id_map|
klass = klass_name.constantize
klass.where(klass.primary_key=>id_map.keys).all do |rec|
id_map[rec.pk].each do |comment|
comment.associations[:author] = rec
end
end
end
end)
end
class User < Sequel::Model
one_to_many :comments, key: :author_id, reciprocal: :author, conditions: {author_type: 'User'},
adder: lambda{|comment| comment.update(author_id: pk, author_type: 'User')},
remover: lambda{|comment| comment.update(author_id: nil, author_type: nil)},
clearer: lambda{comments_dataset.update(author_id: nil, author_type: nil)}
end
class Admin < Sequel::Model
one_to_many :comments, key: :author_id, reciprocal: :author, conditions: {author_type: 'Admin'},
adder: lambda{|comment| comment.update(author_id: pk, author_type: 'Admin')},
remover: lambda{|comment| comment.update(author_id: nil, author_type: nil)},
clearer: lambda{comments_dataset.update(author_id: nil, author_type: nil)}
end
comment = Comment.create
user = User.create
admin = Admin.create
p user.comments
p admin.comments
p comment
comment.author = user
p comment
comment.save
p comment.reload.author
p Comment.eager(:author).all.map{|c| [c, c.associations]}
p User.eager(:comments).all.map{|c| [c, c.associations]}
p Admin.eager(:comments).all.map{|c| [c, c.associations]}
comment.author = admin
p comment
p admin.associations
comment.save
p comment.reload.author
p Comment.eager(:author).all.map{|c| [c, c.associations]}
p User.eager(:comments).all.map{|c| [c, c.associations]}
p Admin.eager(:comments).all.map{|c| [c, c.associations]}
admin.remove_comment(comment)
p comment.reload
user.add_comment(comment)
p comment.reload
user.remove_comment(comment)
p comment.reload
admin.add_comment(comment)
p comment.reload
admin.remove_all_comments
p comment.reload
user.add_comment(comment)
p comment.reload
user.remove_all_comments
p comment.reload |
Beta Was this translation helpful? Give feedback.
0 replies
-
Perfect, Jeremy, worked great! |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Following https://sequel.jeremyevans.net/rdoc/files/doc/advanced_associations_rdoc.html#label-Polymorphic+Associations I`m trying to set the below relations on a legacy Mariadb database, with Sequel 5.53.0 and ruby 3.0.3:
When I run
Comment.last.author
I gotNameError: uninitialized constant Author
from3.0.0/gems/activesupport-7.0.4/lib/active_support/inflector/methods.rb:280:in constantize
.If I set
Author = User
beforeComment.last.author
, it works fine.It looks like lib/sequel/model/associations.rb is not trying to resolve the constant name dinamically., but getting it directly from
many_to_one :author ...
.Hints to fix are welcome.
Beta Was this translation helpful? Give feedback.
All reactions