Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested Attributes doesn't work correctly with Polymorphic Association #1617

Closed
benhutton opened this issue May 2, 2019 · 2 comments
Closed

Comments

@benhutton
Copy link

Complete Description of Issue

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

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.

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`
@benhutton
Copy link
Author

... or would this be a bug with https://github.com/jackdempsey/sequel_polymorphic?

@jeremyevans
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants