Rails iterating through model and changing values doesn't work - ruby-on-rails

I have a PiggyBank model where one of the attributes is interest_modifier(format: decimal). I wrote the following method to change the interest_modifier to a random number:
def set_random_interest_modifier(min = 0.5, max = 1)
self.interest_modifier = rand(min..max).round(2)
self.save!
end
It works. Now, I want a method which iterates over all PiggyBanks and changes the interest_modifier, so i wrote this:
def self.change_interest_modifiers
find_each do |pb|
pb.set_random_interest_modifier
end
end
It iterates over all records but doesn't change the values. Heres the log
[12] pry(main)> PiggyBank.change_interest_modifiers
PiggyBank Load (1.4ms) SELECT "piggy_banks".* FROM "piggy_banks" ORDER BY "piggy_banks"."id" ASC LIMIT 1000
(0.3ms) BEGIN
SQL (1.0ms) UPDATE "piggy_banks" SET "interest_modifier" = $1, "updated_at" = $2 WHERE "piggy_banks"."id" = 7 [["interest_modifier", "0.58"], ["updated_at", "2015-06-02 11:53:33.981262"]]
(3.1ms) COMMIT
(0.4ms) BEGIN
SQL (0.7ms) UPDATE "piggy_banks" SET "interest_modifier" = $1, "updated_at" = $2 WHERE "piggy_banks"."id" = 8 [["interest_modifier", "0.53"], ["updated_at", "2015-06-02 11:53:33.990404"]]
(0.4ms) COMMIT
(0.2ms) BEGIN
SQL (0.7ms) UPDATE "piggy_banks" SET "interest_modifier" = $1, "updated_at" = $2 WHERE "piggy_banks"."id" = 9 [["interest_modifier", "0.78"], ["updated_at", "2015-06-02 11:53:33.994231"]]
(0.4ms) COMMIT
(0.2ms) BEGIN
SQL (0.7ms) UPDATE "piggy_banks" SET "interest_modifier" = $1, "updated_at" = $2 WHERE "piggy_banks"."id" = 10 [["interest_modifier", "0.68"], ["updated_at", "2015-06-02 11:53:33.997966"]]
(0.5ms) COMMIT
(0.2ms) BEGIN
SQL (0.7ms) UPDATE "piggy_banks" SET "interest_modifier" = $1, "updated_at" = $2 WHERE "piggy_banks"."id" = 11 [["interest_modifier", "0.71"], ["updated_at", "2015-06-02 11:53:34.002414"]]
(0.5ms) COMMIT
(0.1ms) BEGIN
(0.1ms) COMMIT
(0.1ms) BEGIN
SQL (0.5ms) UPDATE "piggy_banks" SET "interest_modifier" = $1, "updated_at" = $2 WHERE "piggy_banks"."id" = 13 [["interest_modifier", "0.91"], ["updated_at", "2015-06-02 11:53:34.007100"]]
(0.5ms) COMMIT
(0.1ms) BEGIN
SQL (0.6ms) UPDATE "piggy_banks" SET "interest_modifier" = $1, "updated_at" = $2 WHERE "piggy_banks"."id" = 14 [["interest_modifier", "0.96"], ["updated_at", "2015-06-02 11:53:34.010488"]]
(0.4ms) COMMIT
(0.2ms) BEGIN
SQL (0.6ms) UPDATE "piggy_banks" SET "interest_modifier" = $1, "updated_at" = $2 WHERE "piggy_banks"."id" = 15 [["interest_modifier", "0.8"], ["updated_at", "2015-06-02 11:53:34.014190"]]
(0.5ms) COMMIT
(0.3ms) BEGIN
SQL (0.6ms) UPDATE "piggy_banks" SET "interest_modifier" = $1, "updated_at" = $2 WHERE "piggy_banks"."id" = 16 [["interest_modifier", "0.71"], ["updated_at", "2015-06-02 11:53:34.018658"]]
(0.5ms) COMMIT
(0.2ms) BEGIN
SQL (0.6ms) UPDATE "piggy_banks" SET "interest_modifier" = $1, "updated_at" = $2 WHERE "piggy_banks"."id" = 18 [["interest_modifier", "0.67"], ["updated_at", "2015-06-02 11:53:34.023011"]]
(0.5ms) COMMIT
=> nil
Now, I have no idea why this happens. No errors are raised. Please, help:)
After a few experiments with reloading in console:
p = PiggyBank.first
p.interest_modifier.to_s => "0.69"
PiggyBank.change_interest_modifiers
p.interest_modifier.to_s => "0.69"
reload!
p.interest_modifier.to_s => "0.69"
But, if i do the following, it works:
p = PiggyBank.first
p.interest_modifier.to_s => "0.69"
PiggyBank.change_interest_modifiers
p.interest_modifier.to_s => "0.69"
p.reload
p.interest_modifier.to_s => "0.57"
Why would that be?

This is what's happening:
p = PiggyBank.first
p.interest_modifier.to_s => "0.69"
so we've got an object in memory, p, which we've created and in the act of creation, loaded it's data out of the database.
PiggyBank.change_interest_modifiers
This will iterate through all of the PiggyBank records, creating an object for each. These objects are all different objects to the one that you made already. So, effectively, there have been two different objects made from the same record. The object inside the class method will save the new data to the database, behind p's back if you like.
These objects do not have a "live" connection to the database - that is to say, if the database is changed by a different method, the object in memory does not magically update itself with the new data.
So, while you have p sitting there, something else has changed the data in the database. When you say p.interest_modifier, you're just accessing an instance variable inside the object, you're not accessing the database.
When you then go on to say
p.reload
what you are actually doing is something like this:
p = PiggyBank.find(p.id)
ie, re-setting the contents of the p pointer to point to a new object, created out of the database record. This object now has the up to date data from the database.

Well, the method worked from the start, just forgot to reload the resource :O

Related

terminal output for Rails app produces double requests

My Rails app generates double requests with every page load, and I'm not able to diagnose the source of the problem. Answers here suggest removing all blank/self-referencing hrefs and running rake assets:clean - but neither has any impact.
Both of these requests are identical, save for timestamps and the as part of the controller action processing. The first request says Processing by TradesController#index as HTML whereas the second says Processing by TradesController#index as */*
Has anyone else dealt with this, and if so, how did you fix it?
Started GET "/swaps" for 2600:1700:ba01:ff10:cc31:d814:a204:d70e at 2020-02-23 14:32:25 -0800
Processing by TradesController#index as HTML
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
(0.4ms) SELECT COUNT(*) FROM "trades" WHERE "trades"."trade_requester_id" = $1 AND "trades"."approved_at" IS NULL [["trade_requester_id", 1]]
(0.3ms) SELECT COUNT(*) FROM "trades" WHERE "trades"."trade_recipient_id" = $1 AND "trades"."approved_at" IS NULL [["trade_recipient_id", 1]]
Rendering trades/index.html.erb within layouts/application
Rendered trades/_trades_menu.html.erb (0.5ms)
CACHE (0.0ms) SELECT COUNT(*) FROM "trades" WHERE "trades"."trade_recipient_id" = $1 AND "trades"."approved_at" IS NULL [["trade_recipient_id", 1]]
Rendered trades/index.html.erb within layouts/application (3.0ms)
CACHE (0.0ms) SELECT COUNT(*) FROM "trades" WHERE "trades"."trade_recipient_id" = $1 AND "trades"."approved_at" IS NULL [["trade_recipient_id", 1]]
Transaction Load (0.5ms) SELECT "transactions".* FROM "transactions" WHERE "transactions"."recipient_id" = $1 AND "transactions"."archived_by_recipient" = $2 [["recipient_id", 1], ["archived_by_recipient", false]]
Transaction Load (0.5ms) SELECT "transactions".* FROM "transactions" WHERE "transactions"."sender_id" = $1 AND (state = 'approved' OR state = 'force_flipped' OR state = 'payment_sent' OR state = 'payment_declined' OR state = 'passed' OR state = 'paid' OR state = 'refunded' OR state = 'shipped' OR state = 'received' OR state = 'not_received' OR state = 'unpaid' OR state = 'ignored' OR state = 'declined') AND "transactions"."archived_by_sender" = $2 [["sender_id", 1], ["archived_by_sender", false]]
PaymentTransfer Load (0.3ms) SELECT "payment_transfers".* FROM "payment_transfers" WHERE "payment_transfers"."user_id" = $1 AND (user_id = 1 AND read = FALSE) [["user_id", 1]]
CACHE Transaction Load (0.0ms) SELECT "transactions".* FROM "transactions" WHERE "transactions"."recipient_id" = $1 AND "transactions"."archived_by_recipient" = $2 [["recipient_id", 1], ["archived_by_recipient", false]]
CACHE Transaction Load (0.0ms) SELECT "transactions".* FROM "transactions" WHERE "transactions"."sender_id" = $1 AND (state = 'approved' OR state = 'force_flipped' OR state = 'payment_sent' OR state = 'payment_declined' OR state = 'passed' OR state = 'paid' OR state = 'refunded' OR state = 'shipped' OR state = 'received' OR state = 'not_received' OR state = 'unpaid' OR state = 'ignored' OR state = 'declined') AND "transactions"."archived_by_sender" = $2 [["sender_id", 1], ["archived_by_sender", false]]
CACHE PaymentTransfer Load (0.0ms) SELECT "payment_transfers".* FROM "payment_transfers" WHERE "payment_transfers"."user_id" = $1 AND (user_id = 1 AND read = FALSE) [["user_id", 1]]
(1.2ms) SELECT COUNT(*) FROM "conversations" INNER JOIN "messages" ON "messages"."conversation_id" = "conversations"."id" INNER JOIN "users" ON "users"."id" = "messages"."user_id" WHERE ((conversations.sender_id = 1 OR conversations.receiver_id = 1)) AND (messages.user_id != 1 AND read = FALSE)
Rendered layouts/_header.html.erb (9.2ms)
Completed 200 OK in 58ms (Views: 48.5ms | ActiveRecord: 4.2ms)
(0.2ms) BEGIN
(0.2ms) COMMIT
Started GET "/swaps" for 2600:1700:ba01:ff10:cc31:d814:a204:d70e at 2020-02-23 14:32:26 -0800
Processing by TradesController#index as */*
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
(0.4ms) SELECT COUNT(*) FROM "trades" WHERE "trades"."trade_requester_id" = $1 AND "trades"."approved_at" IS NULL [["trade_requester_id", 1]]
(0.4ms) SELECT COUNT(*) FROM "trades" WHERE "trades"."trade_recipient_id" = $1 AND "trades"."approved_at" IS NULL [["trade_recipient_id", 1]]
Rendering trades/index.html.erb within layouts/application
Rendered trades/_trades_menu.html.erb (0.6ms)
CACHE (0.0ms) SELECT COUNT(*) FROM "trades" WHERE "trades"."trade_recipient_id" = $1 AND "trades"."approved_at" IS NULL [["trade_recipient_id", 1]]
Rendered trades/index.html.erb within layouts/application (27.1ms)
CACHE (0.0ms) SELECT COUNT(*) FROM "trades" WHERE "trades"."trade_recipient_id" = $1 AND "trades"."approved_at" IS NULL [["trade_recipient_id", 1]]
Transaction Load (0.5ms) SELECT "transactions".* FROM "transactions" WHERE "transactions"."recipient_id" = $1 AND "transactions"."archived_by_recipient" = $2 [["recipient_id", 1], ["archived_by_recipient", false]]
Transaction Load (0.6ms) SELECT "transactions".* FROM "transactions" WHERE "transactions"."sender_id" = $1 AND (state = 'approved' OR state = 'force_flipped' OR state = 'payment_sent' OR state = 'payment_declined' OR state = 'passed' OR state = 'paid' OR state = 'refunded' OR state = 'shipped' OR state = 'received' OR state = 'not_received' OR state = 'unpaid' OR state = 'ignored' OR state = 'declined') AND "transactions"."archived_by_sender" = $2 [["sender_id", 1], ["archived_by_sender", false]]
PaymentTransfer Load (0.3ms) SELECT "payment_transfers".* FROM "payment_transfers" WHERE "payment_transfers"."user_id" = $1 AND (user_id = 1 AND read = FALSE) [["user_id", 1]]
CACHE Transaction Load (0.0ms) SELECT "transactions".* FROM "transactions" WHERE "transactions"."recipient_id" = $1 AND "transactions"."archived_by_recipient" = $2 [["recipient_id", 1], ["archived_by_recipient", false]]
CACHE Transaction Load (0.0ms) SELECT "transactions".* FROM "transactions" WHERE "transactions"."sender_id" = $1 AND (state = 'approved' OR state = 'force_flipped' OR state = 'payment_sent' OR state = 'payment_declined' OR state = 'passed' OR state = 'paid' OR state = 'refunded' OR state = 'shipped' OR state = 'received' OR state = 'not_received' OR state = 'unpaid' OR state = 'ignored' OR state = 'declined') AND "transactions"."archived_by_sender" = $2 [["sender_id", 1], ["archived_by_sender", false]]
CACHE PaymentTransfer Load (0.0ms) SELECT "payment_transfers".* FROM "payment_transfers" WHERE "payment_transfers"."user_id" = $1 AND (user_id = 1 AND read = FALSE) [["user_id", 1]]
(0.7ms) SELECT COUNT(*) FROM "conversations" INNER JOIN "messages" ON "messages"."conversation_id" = "conversations"."id" INNER JOIN "users" ON "users"."id" = "messages"."user_id" WHERE ((conversations.sender_id = 1 OR conversations.receiver_id = 1)) AND (messages.user_id != 1 AND read = FALSE)
Rendered layouts/_header.html.erb (8.7ms)
Completed 200 OK in 152ms (Views: 116.6ms | ActiveRecord: 4.1ms)
(0.1ms) BEGIN
(0.1ms) COMMIT

order active_admin column by parent item in belongs_to relationship

I have two models: show_request and show. A show_Request belongs_to a show and a show has_many show_requests. On the show_request page in active_admin, I want to order show_requests by the show's created_at value. Here is my code so far:
ActiveAdmin.register ShowRequest do
controller do
def scoped_collection
end_of_association_chain.includes(:show)
#I also tried ShowRequest.includes(:show)
end
end
index do
column 'Show', sortable: "shows.created_at_asc" do |show_req|
link_to show_req.show.name, admin_show_path(show_req.show)
end
end
end
Here are the server logs:
Started GET "/admin/show_requests" for 127.0.0.1 at 2015-09-18 09:35:36 -0400
Processing by Admin::ShowRequestsController#index as HTML
AdminUser Load (0.3ms) SELECT "admin_users".* FROM "admin_users" WHERE "admin_users"."id" = 1 ORDER BY "admin_users"."id" ASC LIMIT 1
(1.2ms) SELECT COUNT(*) FROM "show_requests" WHERE (not_going_to_show = 'f' OR i_want_my_horse_to_compete = 't')
(0.2ms) SELECT COUNT(*) FROM "show_requests"
(0.2ms) SELECT COUNT(*) FROM "show_requests" WHERE (not_going_to_show = 't' AND i_want_my_horse_to_compete = 'f')
(0.3ms) SELECT COUNT(count_column) FROM (SELECT 1 AS count_column FROM "show_requests" WHERE (not_going_to_show = 'f' OR i_want_my_horse_to_compete = 't') LIMIT 30 OFFSET 0) subquery_for_count
CACHE (0.0ms) SELECT COUNT(count_column) FROM (SELECT 1 AS count_column FROM "show_requests" WHERE (not_going_to_show = 'f' OR i_want_my_horse_to_compete = 't') LIMIT 30 OFFSET 0) subquery_for_count
CACHE (0.0ms) SELECT COUNT(*) FROM "show_requests" WHERE (not_going_to_show = 'f' OR i_want_my_horse_to_compete = 't')
CACHE (0.0ms) SELECT COUNT(count_column) FROM (SELECT 1 AS count_column FROM "show_requests" WHERE (not_going_to_show = 'f' OR i_want_my_horse_to_compete = 't') LIMIT 30 OFFSET 0) subquery_for_count
ShowRequest Load (2.0ms) SELECT "show_requests".* FROM "show_requests" WHERE (not_going_to_show = 'f' OR i_want_my_horse_to_compete = 't') ORDER BY "show_requests"."id" desc LIMIT 30 OFFSET 0
Show Load (9.7ms) SELECT "shows".* FROM "shows" WHERE "shows"."id" IN (2, 1)
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 2]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
Show Load (0.2ms) SELECT "shows".* FROM "shows"
User Load (0.2ms) SELECT "users".* FROM "users"
This is not working. It is not affecting the order of the columns at all. How do I fix this?
Take a look at this part of the log:
ShowRequest Load (2.0ms) SELECT "show_requests".* FROM "show_requests" WHERE (not_going_to_show = 'f' OR i_want_my_horse_to_compete = 't') ORDER BY "show_requests"."id" desc LIMIT 30 OFFSET 0
Show Load (9.7ms) SELECT "shows".* FROM "shows" WHERE "shows"."id" IN (2, 1)
You can see that the ShowRequests and Shows are loaded in separate queries. sortable is not going to work here, because you can't order one table by a field of another without a join. The fix should be to tell ActiveRecord that you need to reference the shows table in your query:
controller do
def scoped_collection
super.includes(:show).references(:shows)
end
end
index do
column :show, sortable: 'shows.created_at'
end
references only works with includes, and forces Rails to perform a join when eager loading.

Active model serializer - wrong number of arguments (0 for 1)

I'm trying to serialize a nested collection. I have a product with images. But when I try a has_many or an explicit method to define an images attribute in my product serializer, I get:
wrong number of arguments (0 for 1)
I could have sworn this was doable. Am I doing something wrong?
Here is my code:
class Api::V1::ProductSerializer < ActiveModel::Serializer
attributes :id, :sku, :name, :description, :images
def images
object.images
end
end
Full backtrace
Started GET "/api/v1/products" for 10.0.2.2 at 2015-03-02 23:12:34 +0000
Processing by Api::V1::ProductsController#index as HTML
User Load (0.7ms) SELECT "users".* FROM "users" INNER JOIN "tenant_users" ON "tenant_users"."user_id" = "users"."id" WHERE "tenant_users"."tenant_id" = 1 AND "users"."authentication_token" = 'pGJE5UWrxzJTEPxMyiez' ORDER BY "users"."id" ASC LIMIT 1
(0.3ms) BEGIN
SQL (0.4ms) UPDATE "users" SET "current_sign_in_at" = $1, "last_sign_in_at" = $2, "sign_in_count" = $3, "updated_at" = $4 WHERE "users"."id" = 1 [["current_sign_in_at", "2015-03-02 23:12:34.913333"], ["last_sign_in_at", "2015-03-02 23:12:33.754114"], ["sign_in_count", 171], ["updated_at", "2015-03-02 23:12:34.914298"]]
(0.7ms) COMMIT
Product Load (0.4ms) SELECT "products".* FROM "products" WHERE "products"."tenant_id" = 1
Attachinary::File Load (0.5ms) SELECT "attachinary_files".* FROM "attachinary_files" WHERE "attachinary_files"."attachinariable_id" = $1 AND "attachinary_files"."attachinariable_type" = $2 AND "attachinary_files"."scope" = 'images' [["attachinariable_id", 1], ["attachinariable_type", "Product"]]
Completed 500 Internal Server Error in 19ms
ArgumentError (wrong number of arguments (0 for 1)):
app/controllers/api/v1/products_controller.rb:4:in `index'
app/controllers/concerns/api/landlord.rb:13:in `require_tenant!'
Rendered /usr/local/rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.4ms)
Rendered /usr/local/rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.1ms)
Rendered /usr/local/rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.1ms)
Rendered /usr/local/rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (11.7ms)

Globalize3 setting same language all over

I have a form where I have 3 edits (one for each language). When I check afterwards, the text doesn't get inserted in the DB ok. (I get 3 times the same text).
This is in the controller of ingredient_category, here it calls the update_other_locals_for after each insert or update, this gets called alright.
after_filter lambda { |controller| controller.update_other_locals_for(#ingredient_category) }, :only => [:create, :update]
This is the code in the ApplicationController that gets called
available_locals.each do |available_locale|
I18n.locale = available_locale
params_object = "#{available_locale}_" + item.class.to_s.underscore.downcase
if params[params_object.to_sym].present?
item.update_attributes(params[params_object.to_sym])
end
end
The update line is getting hit and the correct text is getting to that point but when I'm debugging I see this in the query:
Started PUT "/ingredient_categories/4" for 127.0.0.1 at 2013-06-20 21:58:34 +0200
Processing by IngredientCategoriesController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"aZFuv8v19oZcBkDmUzRzNyMUhBXnL5X0WvSyxNZhEuQ=", "ingredient_category"=>{"name"=>"Dutch"}, "en_ingredient_category"=>{"name"=>"English"}, "fr_ingredient_category"=>{"name"=>"French"}, "id"=>"4"}
Shop Load (0.8ms) SELECT "shops".* FROM "shops" WHERE "shops"."subdomain" = '' LIMIT 1
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 27 LIMIT 1
IngredientCategory Load (0.5ms) SELECT "ingredient_categories".* FROM "ingredient_categories" WHERE "ingredient_categories"."id" = ? LIMIT 1 [["id", "4"]]
CACHE (0.1ms) SELECT "ingredient_categories".* FROM "ingredient_categories" WHERE "ingredient_categories"."id" = ? LIMIT 1 [["id", "4"]]
(0.3ms) begin transaction
IngredientCategory::Translation Load (0.6ms) SELECT "ingredient_category_translations".* FROM "ingredient_category_translations" WHERE "ingredient_category_translations"."ingredient_category_id" = 4
(0.5ms) UPDATE "ingredient_categories" SET "updated_at" = '2013-06-20 19:58:34.880306' WHERE "ingredient_categories"."id" = 4
SQL (3.2ms) INSERT INTO "ingredient_category_translations" ("created_at", "ingredient_category_id", "locale", "name", "updated_at") VALUES (?, ?, ?, ?, ?) [["created_at", Thu, 20 Jun 2013 21:58:34 CEST +02:00], ["ingredient_category_id", 4], ["locale", "en"], ["name", nil], ["updated_at", Thu, 20 Jun 2013 21:58:34 CEST +02:00]]
IngredientCategory::Translation Load (0.5ms) SELECT "ingredient_category_translations".* FROM "ingredient_category_translations" WHERE "ingredient_category_translations"."ingredient_category_id" = 4 AND "ingredient_category_translations"."locale" = 'en' LIMIT 1
(0.7ms) UPDATE "ingredient_category_translations" SET "name" = 'Dutch', "updated_at" = '2013-06-20 19:58:34.918413' WHERE "ingredient_category_translations"."id" = 31
IngredientCategory::Translation Load (0.4ms) SELECT "ingredient_category_translations".* FROM "ingredient_category_translations" WHERE "ingredient_category_translations"."id" = ? LIMIT 1 [["id", 31]]
(2.2ms) commit transaction
Redirected to http://127.0.0.1:3000/ingredient_categories
(0.3ms) begin transaction
(0.6ms) UPDATE "ingredient_categories" SET "updated_at" = '2013-06-20 20:00:23.273350' WHERE "ingredient_categories"."id" = 4
IngredientCategory::Translation Load (0.4ms) SELECT "ingredient_category_translations".* FROM "ingredient_category_translations" WHERE "ingredient_category_translations"."ingredient_category_id" = 4 AND "ingredient_category_translations"."locale" = 'en' LIMIT 1
(0.4ms) UPDATE "ingredient_category_translations" SET "name" = 'English', "updated_at" = '2013-06-20 20:00:23.294591' WHERE "ingredient_category_translations"."id" = 31
IngredientCategory::Translation Load (0.4ms) SELECT "ingredient_category_translations".* FROM "ingredient_category_translations" WHERE "ingredient_category_translations"."id" = ? LIMIT 1 [["id", 31]]
(3.2ms) commit transaction
(0.3ms) begin transaction
(0.5ms) UPDATE "ingredient_categories" SET "updated_at" = '2013-06-20 20:01:19.871801' WHERE "ingredient_categories"."id" = 4
IngredientCategory::Translation Load (0.6ms) SELECT "ingredient_category_translations".* FROM "ingredient_category_translations" WHERE "ingredient_category_translations"."ingredient_category_id" = 4 AND "ingredient_category_translations"."locale" = 'en' LIMIT 1
(0.4ms) UPDATE "ingredient_category_translations" SET "name" = 'French', "updated_at" = '2013-06-20 20:01:19.892798' WHERE "ingredient_category_translations"."id" = 31
IngredientCategory::Translation Load (0.4ms) SELECT "ingredient_category_translations".* FROM "ingredient_category_translations" WHERE "ingredient_category_translations"."id" = ? LIMIT 1 [["id", 31]]
(1.1ms) commit transaction
Completed 302 Found in 187809ms (ActiveRecord: 19.2ms)
I noticed this:
AND "ingredient_category_translations"."locale" = 'en'
on all the updates. Why isn't it setting the corresponding languages?
It seems to be a bad idea to do the updates by changing the I18n.locale. If you do it with Globalize.with_locale(available_locale) it works like a charm!
available_locals.each do |available_locale|
Globalize.with_locale(available_locale) do
params_object = "#{available_locale}_" + item.class.to_s.underscore.downcase
if params[params_object.to_sym].present?
item.update_attributes(params[params_object.to_sym])
end
end
end

Rake Tagging from CSV task to much for SQLite?

I have a rake task that foreach through a CSV file of IDs. I then query the database to see if that id exists in my database. if it exists i use act_as_taggable to add a tag. the script runs fine until it gets to the first match and attempts to write a tag to my SQLite DB. and i get a database locked error. I am wondering if i am hitting the I/O limit of SQLite and need to switch over to a fullblown MySQL db.
if defined?(Rails) && (Rails.env == 'development')
Rails.logger = Logger.new(STDOUT)
end
require 'csv'
desc "Tag Voters that early voted from the Secretary of State Website"
task :tag_early => [:environment] do
file ="db/AllAbsentees.csv"
CSV.foreach(file, :headers=> true) do |row|
#voter_id = row[1]
puts "Working on Row" + row[1]
#voter = Voter.where(:state_id => #voter_id).first()
unless #voter.blank?
#voter.tag_list = "2012_GEN_EARLY_VOTER"
#voter.save()
puts "Voter #" + #voter_id + "tagged"
end
end
end
Voter Load (1.2ms) SELECT "voters".* FROM "voters" WHERE "voters"."state_id" = '00008030' LIMIT 1
ActsAsTaggableOn::Tag Load (0.2ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = 11944 AND "taggings"."taggable_type" = 'Voter' AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL)
(0.1ms) begin transaction
(0.4ms) UPDATE "voters" SET "updated_at" = '2012-11-23 00:02:33.438114' WHERE "voters"."id" = 11944
ActsAsTaggableOn::Tag Load (0.1ms) SELECT "tags".* FROM "tags" WHERE (lower(name) = '2012_gen_early_voter')
ActsAsTaggableOn::Tag Load (0.2ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = 11944 AND "taggings"."taggable_type" = 'Voter' AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL)
ActsAsTaggableOn::Tagging Exists (0.1ms) SELECT 1 AS one FROM "taggings" WHERE ("taggings"."tag_id" = 19 AND "taggings"."taggable_type" = 'Voter' AND "taggings"."taggable_id" = 11944 AND "taggings"."context" = 'tags' AND "taggings"."tagger_id" IS NULL AND "taggings"."tagger_type" IS NULL) LIMIT 1
SQL (1.4ms) INSERT INTO "taggings" ("context", "created_at", "tag_id", "taggable_id", "taggable_type", "tagger_id", "tagger_type") VALUES (?, ?, ?, ?, ?, ?, ?) [["context", "tags"], ["created_at", Fri, 23 Nov 2012 00:02:33 UTC +00:00], ["tag_id", 19], ["taggable_id", 11944], ["taggable_type", "Voter"], ["tagger_id", nil], ["tagger_type", nil]]
(5053.1ms) commit transaction
SQLite3::BusyException: database is locked: commit transaction
(99.7ms) rollback transaction
rake aborted!
SQLite3::BusyException: database is locked: commit transaction
SQLite has not much concurrency; for a transaction to write to the DB, there must be no other reading or writing connections.
Ensure that no other program is reading from or writing to the DB at the same time, and that all DB accesses in your own program use the same database connection.

Resources