Comments (3)
Thanks for the details; they're very helpful.
I think, in this case, defining an after_clone
callback with the restore_attributes(%w[searchable])
should do the trick (restore_attributes docs).
from clowne.
How will ignore differ from nullify? Can you please provide an example?
from clowne.
Nullify will save a null value into the new record. Postgres full text search fields are updated (set) automatically by Postgres and do not accept null update. This generates an error.
Example:
This checklists
table as a full text search fields searchable
(last one). The content of this field is autogenerated by Postgres when creating or updating a record.
Table "public.checklists"
Column | Type | Collation | Nullable | Default
-----------------------+-----------------------------+-----------+----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
id | bigint | | not null | nextval('checklists_id_seq'::regclass)
type | character varying(55) | | not null |
title | character varying(256) | | not null |
description | character varying | | |
is_completed | boolean | | not null | false
...
...
completion_percentage | integer | | not null | 0
searchable | tsvector | | | generated always as (setweight(to_tsvector('english'::regconfig, COALESCE(title, ''::character varying)::text), 'A'::"char") || setweight(to_tsvector('english'::regconfig, COALESCE(description, ''::character varying)::text), 'B'::"char")) stored
If we update to nil we get an error. First let's load the record with the console:
$ rails console
Loading development environment (Rails 6.1.3.2)
[1] pry(main)> checklist = Checklist.first
Checklist Load (2.5ms) SELECT "checklists".* FROM "checklists" ORDER BY "checklists"."id" ASC LIMIT $1 [["LIMIT", 1]]
=> #<ChecklistTemplate:0x000000010f6a7b28>
Then we set the field to nil to simulate clowne nullify
system and we save we have an error:
[2] pry(main)> c.searchable=nil
=> nil
[3] pry(main)> c.save
TRANSACTION (0.6ms) BEGIN
Project Load (1.3ms) SELECT "projects".* FROM "projects" WHERE "projects"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
ChecklistTemplate Update (2.6ms) UPDATE "checklists" SET "updated_at" = $1, "searchable" = $2 WHERE "checklists"."id" = $3 [["updated_at", "2023-05-20 16:54:04.457417"], ["searchable", nil], ["id", 1]]
TRANSACTION (0.5ms) ROLLBACK
ActiveRecord::StatementInvalid: PG::GeneratedAlways: ERROR: column "searchable" can only be updated to DEFAULT
DETAIL: Column "searchable" is a generated column.
from /Users/supercobra/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/rack-mini-profiler-3.1.0/lib/patches/db/pg.rb:69:in `exec_params'
Caused by PG::GeneratedAlways: ERROR: column "searchable" can only be updated to DEFAULT
DETAIL: Column "searchable" is a generated column.
Hence the need to be able to ignore an attribute since nullify does not work in this case.
from clowne.
Related Issues (20)
- Nested params HOT 14
- Multiple Databases HOT 1
- How to access the target cloned parent? HOT 12
- Plans to support belongs to and has many through? HOT 1
- `dependent: :destroy` option on association destroys models related to source after cloning HOT 3
- Is there support for self-referential tables HOT 1
- Weird bug upgrading from 0.2.0 to 1.1.0 HOT 2
- Active Storage & Action Text HOT 4
- Migrate to Standard HOT 3
- Association couldn't be found, Rails 4.1.8 HOT 3
- [Question] belongs_to IDs with nested models HOT 5
- polymorphic association HOT 2
- Is it possible to clone a record to a different table? HOT 3
- Which association is created first if you have multiple associations? HOT 2
- Ruby 3.0 compatibility HOT 5
- Is there possible to use traits in trait?
- Release with mapper access in `after_clone` HOT 2
- Nested include_association duplication HOT 7
- Question HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from clowne.