You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Simplest Possible Self-Contained Example Showing the Bug
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
I expected Address count to be 1, and the Address to have an id set.
Full Backtrace of Exception (if any)
N/A
SQL Log (if any)
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`
The text was updated successfully, but these errors were encountered:
polymorphic is not a plugin that ships with Sequel, so you'll probably want to discuss the issue with the sequel-polymorphic developers (https://github.com/jackdempsey/sequel_polymorphic). Use of polymorphic associations is strongly discouraged by Sequel. I would recommend switching to a non-polymorphic approach, unless you are being charged per-table or per-column.
As this is not a bug in Sequel, I'm going to close this now.
Complete Description of Issue
I have an
Account
with anAddress
. BecauseAddress
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
toAddress::account
), everything worked as expected.Simplest Possible Self-Contained Example Showing the Bug
outputs...
I expected
Address count
to be 1, and theAddress
to have an id set.Full Backtrace of Exception (if any)
N/A
SQL Log (if any)
With logging output to console, the above script outputs the following:
The text was updated successfully, but these errors were encountered: