Using Papertrail's attribute_name_was - ruby-on-rails

I have a request that makes several changes to the same record, however I only need to store a version when one of the attributes changes from false to true. I tried the following:
has_paper_trail on: [:update],
only: {
redone: Proc.new { |srv|
srv.redone == true && srv.redone_was == false
},
}
But I am still getting versions on changes that do not involve the redone attribute changing from false to true... This is an excerpt from the log:
(0.7ms) BEGIN
SQL (11.3ms) UPDATE "servicings" SET "redone" = $1, "redo_reason" = $2, "updated_at" = $3 WHERE "servicings"."id" = $4 [["redone", false], ["redo_reason", nil], ["updated_at", 2018-07-19 22:46:15 UTC], ["id", 218762]]
SQL (9.7ms) INSERT INTO "versions" ("item_type", "item_id", "event", "object", "object_changes", "created_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["item_type", "Servicing"], ["item_id", 218762], ["event", "update"], ["object", "---\nid: 218762\nwork_order_id: 7462\narea_id: 16563\nhigh_priority: false\ncompleted_at: \ncreated_at: 2018-07-10 19:45:36.245612000 Z\nupdated_at: 2018-07-19 22:14:11.143007000 Z\nassignee_id: 90\nclaimed_by_id: \ncompleted_by_id: 90\noccupied_at: \ndeleted_at: \nstatus: open\napproval_status: \nredone: true\nunable_reason: \nredo_reason: testing paper_trail\napproving_user_id: \napproving_user_name: \napproved_at: \n"], ["object_changes", "---\nredone:\n- true\n- false\nredo_reason:\n- testing paper_trail\n- \nupdated_at:\n- 2018-07-19 22:14:11.143007000 Z\n- 2018-07-19 22:46:15.680447100 Z\n"], ["created_at", 2018-07-19 22:46:15 UTC]]
(5.6ms) COMMIT
(2.0ms) BEGIN
(0.8ms) COMMIT
(1.5ms) BEGIN
SQL (9.1ms) UPDATE "servicings" SET "updated_at" = $1, "status" = $2 WHERE "servicings"."id" = $3 [["updated_at", 2018-07-19 22:46:15 UTC], ["status", "completed"], ["id", 218762]]
SQL (2.8ms) INSERT INTO "versions" ("item_type", "item_id", "event", "object", "object_changes", "created_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["item_type", "Servicing"], ["item_id", 218762], ["event", "update"], ["object", "---\nid: 218762\nredone: false\nredo_reason: \nwork_order_id: 7462\narea_id: 16563\nhigh_priority: false\ncompleted_at: \ncreated_at: 2018-07-10 19:45:36.245612000 Z\nupdated_at: 2018-07-19 22:46:15.680447100 Z\nassignee_id: 90\nclaimed_by_id: \ncompleted_by_id: 90\noccupied_at: \ndeleted_at: \nstatus: open\napproval_status: \nunable_reason: \napproving_user_id: \napproving_user_name: \napproved_at: \n"], ["object_changes", "---\nupdated_at:\n- 2018-07-19 22:46:15.680447100 Z\n- 2018-07-19 22:46:15.981721300 Z\nstatus:\n- open\n- completed\n"], ["created_at", 2018-07-19 22:46:15 UTC]]
(8.7ms) COMMIT
WorkOrder Load (15.1ms) SELECT "work_orders".* FROM "work_orders" WHERE "work_orders"."deleted_at" IS NULL AND "work_orders"."id" = $1 LIMIT $2 [["id", 7462], ["LIMIT", 1]]
(1.2ms) BEGIN
SQL (16.3ms) UPDATE "servicings" SET "completed_at" = $1, "updated_at" = $2 WHERE "servicings"."id" = $3 [["completed_at", 2018-07-19 22:46:16 UTC], ["updated_at", 2018-07-19 22:46:16 UTC], ["id", 218762]]
SQL (7.5ms) INSERT INTO "versions" ("item_type", "item_id", "event", "object", "object_changes", "created_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["item_type", "Servicing"], ["item_id", 218762], ["event", "update"], ["object", "---\nid: 218762\nredone: false\nredo_reason: \nwork_order_id: 7462\narea_id: 16563\nhigh_priority: false\ncompleted_at: \ncreated_at: 2018-07-10 19:45:36.245612000 Z\nupdated_at: 2018-07-19 22:46:15.981721300 Z\nassignee_id: 90\nclaimed_by_id: \ncompleted_by_id: 90\noccupied_at: \ndeleted_at: \nstatus: completed\napproval_status: \nunable_reason: \napproving_user_id: \napproving_user_name: \napproved_at: \n"], ["object_changes", "---\ncompleted_at:\n- \n- 2018-07-19 22:46:16.111954100 Z\nupdated_at:\n- 2018-07-19 22:46:15.981721300 Z\n- 2018-07-19 22:46:16.150183500 Z\n"], ["created_at", 2018-07-19 22:46:16 UTC]]
(2.7ms) COMMIT
What could I be doing wrong?

It's probably because PaperTrail's callback executes after the update. If that's the case, redone_was will always be equal to the current redone value.
You should store your redone_was value somewhere before updating.
class Klass
attr_accessor :old_redone_value
before_save :store_old_redone_value
has_paper_trail on: [:update],
only: {
redone: Proc.new { |srv|
srv.redone == true && srv.old_redone_value == false
},
}
def store_old_redone_value
self.old_redone_value = redone_was
end
end
That's the idea. It's not the best solution, and might not work -- but, it'll lead you somewhere.

Related

ActiveStorage gets stuck in recursive attachment create and destroy

I'm trying to attach a js file to a model and store in s3 but every time I run the attach method with file io objects, the server gets hung up in a recursive create and delete of the activestorage attachment database as you can see in these server logs:
ActiveStorage::Blob Create (0.5ms) INSERT INTO "active_storage_blobs" ("key", "filename", "content_type", "metadata", "service_name", "byte_size", "checksum", "created_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id" [["key", "p7bq76rurim77iao2r9a01pj0pwg"], ["filename", "c520252d-a889-4636-ad9c-30973429e25c.js"], ["content_type", "application/javascript"], ["metadata", "{\"identified\":true}"], ["service_name", "amazon"], ["byte_size", 13], ["checksum", "XY+qICmXk3H4sx9RJIwiMA=="], ["created_at", "2022-08-30 21:06:54.911872"]]
↳ app/models/property.rb:22:in `make_js_file'
ActiveStorage::Attachment Create (0.5ms) INSERT INTO "active_storage_attachments" ("name", "record_type", "record_id", "blob_id", "created_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["name", "js_file"], ["record_type", "Property"], ["record_id", "c520252d-a889-4636-ad9c-30973429e25c"], ["blob_id", "e96f2305-a7dd-4e81-b4be-95e8c8d00df5"], ["created_at", "2022-08-30 21:06:54.917626"]]
↳ app/models/property.rb:22:in `make_js_file'
ActiveStorage::Attachment Destroy (0.5ms) DELETE FROM "active_storage_attachments" WHERE "active_storage_attachments"."id" = $1 [["id", "b705726b-b97d-47d6-bb70-de0c9d4dfa6b"]]
↳ app/models/property.rb:22:in `make_js_file'
ActiveStorage::Blob Create (0.3ms) INSERT INTO "active_storage_blobs" ("key", "filename", "content_type", "metadata", "service_name", "byte_size", "checksum", "created_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id" [["key", "zybcvyig2o0u7dgogisklfnig7xv"], ["filename", "c520252d-a889-4636-ad9c-30973429e25c.js"], ["content_type", "application/javascript"], ["metadata", "{\"identified\":true}"], ["service_name", "amazon"], ["byte_size", 13], ["checksum", "XY+qICmXk3H4sx9RJIwiMA=="], ["created_at", "2022-08-30 21:06:54.944469"]]
↳ app/models/property.rb:22:in `make_js_file'
ActiveStorage::Attachment Create (0.3ms) INSERT INTO "active_storage_attachments" ("name", "record_type", "record_id", "blob_id", "created_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["name", "js_file"], ["record_type", "Property"], ["record_id", "c520252d-a889-4636-ad9c-30973429e25c"], ["blob_id", "23e176a4-3816-47fe-97a5-e2bb3af97a1d"], ["created_at", "2022-08-30 21:06:54.947629"]]
↳ app/models/property.rb:22:in `make_js_file'
ActiveStorage::Attachment Destroy (0.3ms) DELETE FROM "active_storage_attachments" WHERE "active_storage_attachments"."id" = $1 [["id", "7d0e1765-4d6a-46ed-8b29-012adecda684"]]
↳ app/models/property.rb:22:in `make_js_file'
ActiveStorage::Blob Create (0.4ms) INSERT INTO "active_storage_blobs" ("key", "filename", "content_type", "metadata", "service_name", "byte_size", "checksum", "created_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id" [["key", "8997y96c81r8fxcofbe854ivu2r1"], ["filename", "c520252d-a889-4636-ad9c-30973429e25c.js"], ["content_type", "application/javascript"], ["metadata", "{\"identified\":true}"], ["service_name", "amazon"], ["byte_size", 13], ["checksum", "XY+qICmXk3H4sx9RJIwiMA=="], ["created_at", "2022-08-30 21:06:54.965508"]]
↳ app/models/property.rb:22:in `make_js_file'
ActiveStorage::Attachment Create (0.4ms) INSERT INTO "active_storage_attachments" ("name", "record_type", "record_id", "blob_id", "created_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["name", "js_file"], ["record_type", "Property"], ["record_id", "c520252d-a889-4636-ad9c-30973429e25c"], ["blob_id", "4646e337-2ae3-4afc-aab0-48f84af4657d"], ["created_at", "2022-08-30 21:06:54.969796"]]
↳ app/models/property.rb:22:in `make_js_file'
ActiveStorage::Attachment Destroy (2.2ms) DELETE FROM "active_storage_attachments" WHERE "active_storage_attachments"."id" = $1 [["id", "c687a1fa-7ba3-4e76-af20-a3867c153b95"]]
↳ app/models/property.rb:22:in `make_js_file'
ActiveStorage::Blob Create (0.5ms) INSERT INTO "active_storage_blobs" ("key", "filename", "content_type", "metadata", "service_name", "byte_size", "checksum", "created_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id" [["key", "83hcoeo5mdwo4glg3gjmb7jx2l34"], ["filename", "c520252d-a889-4636-ad9c-30973429e25c.js"], ["content_type", "application/javascript"], ["metadata", "{\"identified\":true}"], ["service_name", "amazon"], ["byte_size", 13], ["checksum", "XY+qICmXk3H4sx9RJIwiMA=="], ["created_at", "2022-08-30 21:06:54.998981"]]
↳ app/models/property.rb:22:in `make_js_file'
ActiveStorage::Attachment Create (0.5ms) INSERT INTO "active_storage_attachments" ("name", "record_type", "record_id", "blob_id", "created_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["name", "js_file"], ["record_type", "Property"], ["record_id", "c520252d-a889-4636-ad9c-30973429e25c"], ["blob_id", "9e053cf2-a248-44b9-bf37-b3005efea1be"], ["created_at", "2022-08-30 21:06:55.004691"]]
↳ app/models/property.rb:22:in `make_js_file'
ActiveStorage::Attachment Destroy (0.3ms) DELETE FROM "active_storage_attachments" WHERE "active_storage_attachments"."id" = $1 [["id", "5bc44cce-62cf-4dc5-829e-97dfd9b28411"]]
↳ app/models/property.rb:22:in `make_js_file'
ActiveStorage::Blob Create (0.4ms) INSERT INTO "active_storage_blobs" ("key", "filename", "content_type", "metadata", "service_name", "byte_size", "checksum", "created_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id" [["key", "a4c22dupjyndviedp41r6e8cg7dx"], ["filename", "c520252d-a889-4636-ad9c-30973429e25c.js"], ["content_type", "application/javascript"], ["metadata", "{\"identified\":true}"], ["service_name", "amazon"], ["byte_size", 13], ["checksum", "XY+qICmXk3H4sx9RJIwiMA=="], ["created_at", "2022-08-30 21:06:55.029049"]]
↳ app/models/property.rb:22:in `make_js_file'
I thought maybe where I run the attach method might affect it so I tried adding it in the model, controller, and tested in the console and I get the same result.
I also tried using File, Temfile, and StringIO but get the same result.
Here is an example
dir = Rails.root.join("tmp/files")
file = File.open("#{dir}/#{self.id}.js", "w") { |f| f.write "function(){};" }
self.js_file.attach(
io: File.open("#{dir}/#{self.id}.js"),
filename: "#{self.id}.js",
content_type: 'application/javascript'
)
Not sure what the problem might be.
I made a mistake. It seems like adding it in the model with a callback created a recursive loop when the attach method is applied to the model. Better to leave out of the model

unable to upload file/image rails comfortable mexican sofa cms

I integrated comfortable-mexican-sofa CMS with my rails 6.0 app. Everything working fine. But file upload functionality is not working.
The server is continuously running creating and deleting ActiveStorage::Blob and ActiveStorage::Attachment
ActiveStorage::Blob Create (0.8ms) INSERT INTO "active_storage_blobs" ("key", "filename", "content_type", "metadata", "byte_size", "checksum", "created_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["key", "56qutde9f90cknxanwuqnivgv2eo"], ["filename", "g500-black-back.jpg"], ["content_type", "image/jpeg"], ["metadata", "{\"identified\":true}"], ["byte_size", 2223], ["checksum", "EyM+/P6Ccq9Pw69YV7fAww=="], ["created_at", "2019-09-30 11:26:38.992295"]]
ActiveStorage::Attachment Create (2.6ms) INSERT INTO "active_storage_attachments" ("name", "record_type", "record_id", "blob_id", "created_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["name", "attachment"], ["record_type", "Comfy::Cms::File"], ["record_id", 6], ["blob_id", 356], ["created_at", "2019-09-30 11:26:39.029294"]]
(0.4ms) SELECT "comfy_cms_categories"."id" FROM "comfy_cms_categories" WHERE "comfy_cms_categories"."categorized_type" = $1 [["categorized_type", "Comfy::Cms::File"]]
Comfy::Cms::Categorization Load (0.4ms) SELECT "comfy_cms_categorizations".* FROM "comfy_cms_categorizations" WHERE "comfy_cms_categorizations"."categorized_id" = $1 AND "comfy_cms_categorizations"."categorized_type" = $2 AND "comfy_cms_categorizations"."category_id" = $3 [["categorized_id", 6], ["categorized_type", "Comfy::Cms::File"], ["category_id", 1]]
ActiveStorage::Attachment Destroy (0.7ms) DELETE FROM "active_storage_attachments" WHERE "active_storage_attachments"."id" = $1 [["id", 355]]
ActiveStorage::Blob Create (0.7ms) INSERT INTO "active_storage_blobs" ("key", "filename", "content_type", "metadata", "byte_size", "checksum", "created_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["key", "so1r6i8qli6p5etiz1fz1jee4hcy"], ["filename", "g500-black-back.jpg"], ["content_type", "image/jpeg"], ["metadata", "{\"identified\":true}"], ["byte_size", 2223], ["checksum", "EyM+/P6Ccq9Pw69YV7fAww=="], ["created_at", "2019-09-30 11:26:39.113055"]]
ActiveStorage::Attachment Create (0.9ms) INSERT INTO "active_storage_attachments" ("name", "record_type", "record_id", "blob_id", "created_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["name", "attachment"], ["record_type", "Comfy::Cms::File"], ["record_id", 6], ["blob_id", 357], ["created_at", "2019-09-30 11:26:39.130977"]]
(0.4ms) SELECT "comfy_cms_categories"."id" FROM "comfy_cms_categories" WHERE "comfy_cms_categories"."categorized_type" = $1 [["categorized_type", "Comfy::Cms::File"]]
Comfy::Cms::Categorization Load (0.4ms) SELECT "comfy_cms_categorizations".* FROM "comfy_cms_categorizations" WHERE "comfy_cms_categorizations"."categorized_id" = $1 AND "comfy_cms_categorizations"."categorized_type" = $2 AND "comfy_cms_categorizations"."category_id" = $3 [["categorized_id", 6], ["categorized_type", "Comfy::Cms::File"], ["category_id", 1]]
ActiveStorage::Attachment Destroy (0.7ms) DELETE FROM "active_storage_attachments" WHERE "active_storage_attachments"."id" = $1 [["id", 356]]

What is the fastest way to compare two values from different hashes?

I am trying to compare two hashes to see if the values match so i can reconcile some data.
I have a hash of existing accounts and a hash of accounts i need to check if they exist based on the existing accounts to see if it needs to be inserted.
# Reconcile Adaccounts
def self.reconcileAdaccounts(ad_accounts, user_id)
# These are the accounts that exist
existing_accounts = FbAdaccount.active
.select("fb_id")
.where("user_id = ?", user_id)
.as_json
ad_accounts.each do |ad_account|
if #here i would need to check if ad_account["account_id"] matches one of the existing records
p "This one already exists"
p ad_account["account_id"]
else
p "I need to create this one"
p ad_account["account_id"]
end
end
end
I could have nested loops but that does not seem like it would be the best solution, i also tried looking for any rails API functions that could do this but i have not found one.
What is the most efficient way of doing this?
What you're looking for is find_or_create_by. I've additionally wrapped the call in a transaction so all of this hits the database just once — in a single query.
def self.reconcileAdaccounts(ad_accounts, user_id)
FbAdaccount.transaction do
ad_accounts.each do |ad_account|
FbAdaccount.active
.select('fb_id')
.where('user_id = ?', user_id)
.find_or_create_by(ad_account['account_id'])
end
end
end
Transactions
A transaction doesn't magically alter the query, but it wraps it together so that it either all happens together, or doesn't happen at all.
With the following code:
numbers = (3..7).to_a
Ear.transaction do
numbers.each do |number|
Ear.find_or_create_by(bla: number)
end
end
Note the BEGIN and COMMIT statements. They signify where the call starts and ends.
(0.1ms) BEGIN
Ear Load (0.5ms) SELECT "ears".* FROM "ears" WHERE "ears"."bla" = $1 LIMIT $2 [["bla", 3], ["LIMIT", 1]]
Ear Create (0.8ms) INSERT INTO "ears" ("bla", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["bla", 3], ["created_at", "2019-02-05 23:17:28.157989"], ["updated_at", "2019-02-05 23:17:28.157989"]]
Ear Load (0.2ms) SELECT "ears".* FROM "ears" WHERE "ears"."bla" = $1 LIMIT $2 [["bla", 4], ["LIMIT", 1]]
Ear Create (0.2ms) INSERT INTO "ears" ("bla", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["bla", 4], ["created_at", "2019-02-05 23:17:28.160838"], ["updated_at", "2019-02-05 23:17:28.160838"]]
Ear Load (0.1ms) SELECT "ears".* FROM "ears" WHERE "ears"."bla" = $1 LIMIT $2 [["bla", 5], ["LIMIT", 1]]
Ear Create (0.2ms) INSERT INTO "ears" ("bla", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["bla", 5], ["created_at", "2019-02-05 23:17:28.162241"], ["updated_at", "2019-02-05 23:17:28.162241"]]
Ear Load (0.2ms) SELECT "ears".* FROM "ears" WHERE "ears"."bla" = $1 LIMIT $2 [["bla", 6], ["LIMIT", 1]]
Ear Create (0.2ms) INSERT INTO "ears" ("bla", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["bla", 6], ["created_at", "2019-02-05 23:17:28.163794"], ["updated_at", "2019-02-05 23:17:28.163794"]]
Ear Load (0.2ms) SELECT "ears".* FROM "ears" WHERE "ears"."bla" = $1 LIMIT $2 [["bla", 7], ["LIMIT", 1]]
Ear Create (0.2ms) INSERT INTO "ears" ("bla", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["bla", 7], ["created_at", "2019-02-05 23:17:28.165589"], ["updated_at", "2019-02-05 23:17:28.165589"]]
(40.9ms) COMMIT
Now here's the same code, just without a transaction to wrap it in:
numbers = (20..25).to_a
numbers.each do |number|
Ear.find_or_create_by(bla: number)
end
Note the total number BEGIN and COMMIT messages:
Ear Load (249.3ms) SELECT "ears".* FROM "ears" WHERE "ears"."bla" = $1 LIMIT $2 [["bla", 20], ["LIMIT", 1]]
(73.4ms) BEGIN
Ear Create (48.7ms) INSERT INTO "ears" ("bla", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["bla", 20], ["created_at", "2019-02-06 00:12:35.754374"], ["updated_at", "2019-02-06 00:12:35.754374"]]
(79.8ms) COMMIT
Ear Load (0.4ms) SELECT "ears".* FROM "ears" WHERE "ears"."bla" = $1 LIMIT $2 [["bla", 21], ["LIMIT", 1]]
(0.2ms) BEGIN
Ear Create (77.7ms) INSERT INTO "ears" ("bla", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["bla", 21], ["created_at", "2019-02-06 00:12:35.918461"], ["updated_at", "2019-02-06 00:12:35.918461"]]
(0.3ms) COMMIT
Ear Load (0.2ms) SELECT "ears".* FROM "ears" WHERE "ears"."bla" = $1 LIMIT $2 [["bla", 22], ["LIMIT", 1]]
(0.2ms) BEGIN
Ear Create (2.7ms) INSERT INTO "ears" ("bla", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["bla", 22], ["created_at", "2019-02-06 00:12:36.003822"], ["updated_at", "2019-02-06 00:12:36.003822"]]
(110.1ms) COMMIT
Ear Load (0.3ms) SELECT "ears".* FROM "ears" WHERE "ears"."bla" = $1 LIMIT $2 [["bla", 23], ["LIMIT", 1]]
(0.2ms) BEGIN
Ear Create (0.3ms) INSERT INTO "ears" ("bla", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["bla", 23], ["created_at", "2019-02-06 00:12:36.125187"], ["updated_at", "2019-02-06 00:12:36.125187"]]
(37.0ms) COMMIT
Ear Load (0.2ms) SELECT "ears".* FROM "ears" WHERE "ears"."bla" = $1 LIMIT $2 [["bla", 24], ["LIMIT", 1]]
(0.2ms) BEGIN
Ear Create (0.4ms) INSERT INTO "ears" ("bla", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["bla", 24], ["created_at", "2019-02-06 00:12:36.168414"], ["updated_at", "2019-02-06 00:12:36.168414"]]
(72.4ms) COMMIT
Ear Load (0.3ms) SELECT "ears".* FROM "ears" WHERE "ears"."bla" = $1 LIMIT $2 [["bla", 25], ["LIMIT", 1]]
(1.7ms) BEGIN
Ear Create (0.4ms) INSERT INTO "ears" ("bla", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["bla", 25], ["created_at", "2019-02-06 00:12:36.249285"], ["updated_at", "2019-02-06 00:12:36.249285"]]
(121.2ms) COMMIT

Setting value as params on creation

Attempting to make different orders where depending on the order there is different revisions_left which is an integer in the migration file.
I pass the params[:orderref] through a button like so <%= link_to 'Package 1', new_order_path(orderref: 1)%> and then am trying to call it in the create method like so
if params[:orderref] == "1"
#order.revisions_left = 1
elsif params[:orderref] == "2"
#order.revisions_left = 2
elsif params[:orderref] == "3"
#order.revisions_left = 3
end
However the revisions_left fails to save and am unsure why
Edit: Logs
Processing by OrderController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"VGt/2PCPMmVnzXGm+UgHcmSvX7FEHxOoF/TfJoKf8gn3NJ0grD9JkydfswwV50GXIneFuTSClLK7cqvpsRCwRg==", "order"=>{"name"=>"sdada", "email"=>"as#tt.com", "company"=>"", "event_type"=>"Birthday", "country"=>"Brazil", "description"=>"1233 1 123 11", "order_type"=>"3", "price"=>"80"}, "commit"=>"Create Order"}
Order Load (3.0ms) SELECT "orders".* FROM "orders" WHERE "orders"."name" = $1 AND "orders"."company" = $2 AND "orders"."email" = $3 AND "orders"."event_type" = $4 AND "orders"."country" = $5 AND "orders"."description" = $6 AND "orders"."order_type" = $7 AND "orders"."price" = $8 ORDER BY "orders"."id" ASC LIMIT $9 [["name", "sdada"], ["company", ""], ["email", "as#tt.com"], ["event_type", "Birthday"], ["country", "Brazil"], ["description", "1233 1 123 11"], ["order_type", 3], ["price", 80], ["LIMIT", 1]]
(0.4ms) BEGIN
Order Exists (1.0ms) SELECT 1 AS one FROM "orders" WHERE LOWER("orders"."email") = LOWER($1) LIMIT $2 [["email", "as#tt.com"], ["LIMIT", 1]]
SQL (6.2ms) INSERT INTO "orders" ("name", "company", "email", "event_type", "country", "description", "price", "order_type", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING "id" [["name", "sdada"], ["company", ""], ["email", "as#tt.com"], ["event_type", "Birthday"], ["country", "Brazil"], ["description", "1233 1 123 11"], ["price", 80], ["order_type", 3], ["created_at", 2017-08-03 05:26:21 UTC], ["updated_at", 2017-08-03 05:26:21 UTC]]
(71.2ms) COMMIT
(0.1ms) BEGIN
Order Exists (0.5ms) SELECT 1 AS one FROM "orders" WHERE LOWER("orders"."email") = LOWER($1) AND ("orders"."id" != $2) LIMIT $3 [["email", "as#tt.com"], ["id", 3], ["LIMIT", 1]]
SQL (0.7ms) UPDATE "orders" SET "status" = $1, "updated_at" = $2 WHERE "orders"."id" = $3 [["status", 1], ["updated_at", 2017-08-03 05:26:22 UTC], ["id", 3]]
(0.3ms) COMMIT

How to pass time_select in controller params?

<%= f.time_select :exact, {ampm: true, :discard_minute=> true} %> # t.time "exact"
How do I pass the above in params?
session[:challenge_exact] = params[:exact].parse_time_select! # got nil error
session[:challenge_exact] = challenge_params[:exact]
session[:challenge_exact] = [params["challenge"]["exact(1i)"], params["challenge"]["exact(2i)"], params["challenge"]["exact(3i)"], params["challenge"]["exact(4i)"], params["challenge"]["exact(5i)"]].join(',')
None of the above work. After the challenge is created I see that exact: nil even though it was populated in the form.
Started POST "/challenges" for ::1 at 2017-05-02 08:28:31 -0400
Processing by ChallengesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"oN+DzJK+/GGmpTOOVYMUJQdywuENwxhmX/EVeW6NSZ3w1MctoMd/VHS39d+JasEjHujO88nLPSB2qUKEhet5yA==", "challenge"=>{"name"=>"Write 3 Gratitudes", "categorization"=>"health", "category"=>"goal", "deadline(2i)"=>"6", "deadline(3i)"=>"2", "deadline(1i)"=>"2017", "push"=>"0", "message"=>"1", "mail"=>"0", "remind"=>["mon", "thu", ""], "exact(1i)"=>"2017", "exact(2i)"=>"5", "exact(3i)"=>"2", "exact(4i)"=>"08", "exact(5i)"=>"27", "conceal"=>"0"}, "number"=>"", "email"=>"", "button"=>""}
Redirected to http://localhost:3000/signup
Completed 302 Found in 47ms (ActiveRecord: 0.0ms)
After POST challenge a person then creates a user account...
Started POST "/users" for ::1 at 2017-05-03 01:02:13 -0400
Processing by UsersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"4Lv5NSfH9tMnC9+t5bVC5x1d571y0jhwsgvYU3t2sp85XAnNOsHk+FCmtGcj2wJVysV6IlAIzF4jCx1hIofwsNg==", "user"=>{"name"=>"test", "last_name"=>"daddy", "email"=>"testdaddy#gmail.com", "password"=>"[FILTERED]", "time_zone"=>"Eastern Time (US & Canada)", "subscribe"=>"0"}, "button"=>""}
(0.2ms) BEGIN
User Exists (0.5ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('testdaddy#gmail.com') LIMIT 1
SQL (2.3ms) INSERT INTO "users" ("time_zone", "name", "last_name", "subscribe", "email", "password_digest", "number", "created_at", "updated_at", "activation_digest", "activated") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING "id" [["time_zone", "Eastern Time (US & Canada)"], ["name", "test"], ["last_name", "daddy"], ["subscribe", "f"], ["email", "testdaddy#gmail.com"], ["password_digest", "$2a$10$JSSPUl0JDnUXybnAuSnEO.q5DgZscaU3hRpcM4C5L0a0aEtflzb7ne"], ["number", "15169499507"], ["created_at", "2017-05-03 05:02:14.144232"], ["updated_at", "2017-05-03 05:02:14.144232"], ["activation_digest", "$2a$10$NDtWKvxuJm/Hb9YIOzuVIuPpttCZwjsgYws6HhEcbHc.PukxXNpeLa"], ["activated", "t"]]
(18.8ms) COMMIT
(0.2ms) BEGIN
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 16]]
# AS YOU CAN SEE WITH INSERT INTO CHALLENGES FOR SOME REASON THE SESSION PARAMS FOR EXACT ISN'T PASSING FOR EXACT
SQL (0.6ms) INSERT INTO "challenges" ("committed", "name", "categorization", "category", "deadline", "remind", "message", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING "id" [["committed", nil], ["name", "Write 3 Gratitudes"], ["categorization", "health"], ["category", "goal"], ["deadline", "2017-06-03"], ["remind", "---\n- mon\n- thu\n- ''\n"], ["message", "t"], ["user_id", 16], ["created_at", "2017-05-03 05:02:14.434251"], ["updated_at", "2017-05-03 05:02:14.434251"]]
ActsAsTaggableOn::Tag Load (0.6ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = $1 AND "taggings"."taggable_type" = $2 AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL) [["taggable_id", 40], ["taggable_type", "Challenge"]]
ActsAsTaggableOn::Tag Load (0.8ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = $1 AND "taggings"."taggable_type" = $2 AND (taggings.context = 'tags' AND
taggings.tagger_id = 16 AND
taggings.tagger_type = 'User') [["taggable_id", 40], ["taggable_type", "Challenge"]]
(17.2ms) COMMIT
Redirected to http://localhost:3000/
How it appears in console...
exact: 2000-01-01 02:32:00 UTC>
You can access exact params like:
> params[:challenge]["exact(1i)"]
#=> "2017"
> params[:challenge]["exact(2i)"]
#=> "5"
> params[:challenge]["exact(3i)"]
#=> "2"
> params[:challenge]["exact(4i)"]
#=> "08"
> params[:challenge]["exact(5i)"]
#=> "27"
You can parse time like this:
exact_time = "#{params[:challenge]['exact(4i)']}:#{params[:challenge]['exact(5i)']}"
#=> "08:27"
> session[:challenge_exact] = Time.parse(exact_time)
#=> 2017-05-03 08:27:00 +0530
I would do:
session[:challenge_exact] = components_to_absolute(params[:exact])
def components_to_absolute(components)
DateTime.new(components['exact(1i)'].to_i,
components['exact(2i)'].to_i,
components['exact(3i)'].to_i,
components['exact(4i)'].to_i,
components['exact(5i)'].to_i)
end

Resources