Rails don't rollback and don't update attribute either - ruby-on-rails

In the terminal (later I need to add this to the migration) I want to update total_campaign_codes_amount which represents all campaign codes for each credit campaign. This field is a counter_cache and to do so in terminal I've got this script:
CreditCampaign.all.each do |cc|
cc.update!(
total_campaign_codes_amount: cc.campaign_codes.count
)
end
I didn't received any errors in my sql, everything should worked but these values are not saved to - total_campaign_codes_amount for each CreditCampaign shows 0.
CreditCampaign Update (0.2ms) UPDATE "credit_campaigns" SET "updated_at" = $1 WHERE "credit_campaigns"."id" = $2 [["updated_at", "2019-10-14 15:26:54.010779"], ["id", 1]]
(0.4ms) COMMIT
(0.2ms) SELECT COUNT(*) FROM "campaign_codes" WHERE "campaign_codes"."credit_campaign_id" = $1 [["credit_campaign_id", 2]]
(0.1ms) BEGIN
CreditCampaign Update (0.2ms) UPDATE "credit_campaigns" SET "updated_at" = $1 WHERE "credit_campaigns"."id" = $2 [["updated_at", "2019-10-14 15:26:54.013239"], ["id", 2]]
(0.2ms) COMMIT
=> [#<CreditCampaign id: 1, amount: 0.25e3, interest_rate: 0.12e2, installment_amount: 0.1e3, duration_in_months: 24, start_date: "2019-10-10", end_date: "2019-11-10", created_at: "2019-10-10 14:54:27", updated_at: "2019-10-14 15:26:54", name: "Credit Campaign #1", total_campaign_codes_amount: 15, used_campaign_codes_amount: 6>, #<CreditCampaign id: 2, amount: 0.1e1, interest_rate: 0.1e1, installment_amount: 0.1e1, duration_in_months: 1, start_date: "2011-01-01", end_date: "2020-01-01", created_at: "2019-10-14 13:40:02", updated_at: "2019-10-14 15:26:54", name: "test", total_campaign_codes_amount: 10, used_campaign_codes_amount: 0>]
I was trying to use update_attributes! instead of update! and find_each instead of all.each but without any results.

You can not update counter cache column in Rails, because it is added to the containing model's list of read-only attributes through attr_readonly. Instead you should use update_counters or reset_counters.

Related

Different result in rails console and in puts

When I use puts(#participantt = Participant.where(id: 1)) then in the console I get
Participant Load (0.3ms) SELECT "participants".* FROM "participants" WHERE "participants"."id" = $1 [["id", 1]]
↳ app/controllers/interviews_controller.rb:119:in `puts'
#<Participant:0x000000000c778bf0>
But if I type #participantt = Participant.where(id: 1) in rails console then I get
Participant Load (0.7ms) SELECT "participants".* FROM "participants" WHERE "participants"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<Participant id: 1, name: "Ram", email: "Ram#g.com", created_at: "2020-05-08 08:19:00", updated_at: "2020-05-08 08:19:00">]>
Why is this so?
puts calls to_s on before printing result of expression(which will be an object) passed to it. It generally prints class name with it's object id.
Here, result of #participant = Participant.where(id: 1) is the Participant with id and get stored in instance variable #participant
Passing #participant to puts will first call #participant.to_s before printing it.
In case of just #participant = Participant.where(id: 1), the console shows the result which is typical REPL utility does. And if you do puts #participant there after then you will again get same as what you get with puts in you question.

Rails scope doesn't return correct data

When I use scope which I prepared in model, rails returns incorrect data.
My Model:
class CurrencyRate < ActiveRecord::Base
scope :eur_today, -> {where(currency: "eur").where(date: Time.now.in_time_zone.to_date).first}
end
Inforamation from rails console:
2.3.0 :011 > CurrencyRate.eur_today
CurrencyRate Load (0.2ms) SELECT "currency_rates".* FROM
"currency_rates" WHERE "currency_rates"."currency" = ? AND
"currency_rates"."date" = ? ORDER BY "currency_rates"."id" ASC LIMIT 1
[["currency", "eur"], ["date", "2017-08-09"]]
CurrencyRate Load (0.2ms) SELECT "currency_rates".* FROM
"currency_rates"
=> #<ActiveRecord::Relation [#<CurrencyRate id: 2, currency: "eur",
sale: 4.248546249999998, purchase: 4.265333125, date: "2017-08-08",
created_at: "2017-08-08 20:52:08", updated_at: "2017-08-08 20:54:10",
sale_percentage_diff: nil, purchase_percentage_diff: nil>]>
When I use the same query like in scope, returned data is correct:
2.3.0 :012 > CurrencyRate.where(currency: "eur").where(date: Time.now.in_time_zone.to_date).first
CurrencyRate Load (0.2ms) SELECT "currency_rates".* FROM
"currency_rates" WHERE "currency_rates"."currency" = ? AND
"currency_rates"."date" = ? ORDER BY "currency_rates"."id" ASC LIMIT 1
[["currency", "eur"], ["date", "2017-08-09"]]
=> nil
Why scope doesn't work correct?
Because scopes must return an ActiveRecord::Relation. Get rid of the first call in your scope and use that outside the scope. Why? Because scopes have to be chainable.

How do I write an array of attributes to a column after having first cycled through a collection?

So basically what I want to happen is, on my n.parents attribute I would like to set a value like [val1, val2, val3, val4].
My setter method looks like this:
def parents=(*parents)
write_attribute(self.base_class.ancestry_column,
if parents.nil?
nil
else
parents.map(&:child_ancestry)
end
)
end
But when I run this, I get this error:
> n.parents= a,b
NoMethodError: undefined method `child_ancestry' for #<Array:0x007f9fb0072fb8>
In this case, val1 = a.child_ancestry, val2 = b.child_ancestry...but in theory, I should be able to do n.parents= a,b,c,d,e,f and it should work just as well.
P.S. I am trying to write these to the ancestry_column of the base_class of the object I am updating.
Edit 1
After trying both answers below from #zealoushacker and #nathanvda, I keep getting the same undefined method 'child_ancestry' for #<Array....> error.
However, if in my console I just do any of those map operations, it seems to return fine...so I am even more confused.
Example:
[8] pry(main)> n
=> #<Node id: 36, family_tree_id: 2, created_at: "2015-01-28 23:19:28", updated_at: "2015-01-28 23:19:28", name: "Mesty", ancestry: "13/35", ancestry_depth: 0, max_tree_depth: 0>
[9] pry(main)> n.parents
Node Load (0.4ms) SELECT "nodes".* FROM "nodes" WHERE "nodes"."id" = $1 LIMIT 1 [["id", 35]]
=> [#<Node id: 35, family_tree_id: 2, created_at: "2015-01-28 23:17:36", updated_at: "2015-01-28 23:17:36", name: "Testy", ancestry: "13", ancestry_depth: 0, max_tree_depth: 0>]
[10] pry(main)> n.parents.map(&:child_ancestry)
Node Load (0.4ms) SELECT "nodes".* FROM "nodes" WHERE "nodes"."id" = $1 LIMIT 1 [["id", 35]]
=> ["13/35"]
[11] pry(main)> n.parents.flatten.map(&:child_ancestry)
Node Load (0.3ms) SELECT "nodes".* FROM "nodes" WHERE "nodes"."id" = $1 LIMIT 1 [["id", 35]]
=> ["13/35"]
You need to write:
parents.flatten.map(&:child_ancestry)
because the *parents splat argument contains an array of arrays, which looks something like this if you inspect it:
[[#<ChildAncestry:0x000001018d1010>, #<ChildAncestry:0x000001018d0fe8>]]
Take a look at Array#flatten.
It takes the above and converts it to something like:
[#<ChildAncestry:0x000001018d1010>, #<ChildAncestry:0x000001018d0fe8>]
on which you may then use map as you had.
Weird to use the splat operator in an assignment, why not do something like
def parents=(new_parents)
ancestry = if new_parents.nil?
nil
else
new_parents = [new_parents] unless new_parents.is_a?(Array)
new_parents.map(&:child_ancestry).join('/')
end
write_attribute(self.base_class.ancestry_column, ancestry)
end
and then you can still write
n.parents = a,b
(which is converted to an array automatically in the assignment).
The splat operator will wrap the given parameter in an array again. So just drop the splat operator. It is used in function calls, on assignments it does not make any sense imho.
trying it manually:
The thing you need to try in the console to see if it works:
> my_parents = a,b
> ancestry = my_parents.map(&:child_ancestry)
> n.ancestry_column = ancestry

How to check for database changes of in-memory records?

I want to check if an ActiveRecord instance was changed database-wise. Something like:
p1 = Product.first
p1.name #=> "some name"
p2 = Product.first
p2.name = "other name"
p2.save #=> true
p1.database_changed? #=> true
I'm currently comparing the record's attributes to the persisted attributes:
class Product < ActiveRecord::Base
def database_changed?
Product.find(id).attributes != attributes
end
end
This seems to work, but I'm wondering if there is a built-in way to find database changes?
After Зелёный's comment I've reviewed ActiveModel::Dirty and realized that it almost does what I want. There's already an in-memory state (the record's attributes) and a database state (handled by ActiveModel::Dirty). I just need a method to update the database state, leaving the in-memory state unchanged:
def refresh
#changed_attributes = {}
fresh_object = self.class.unscoped { self.class.find(id) }
fresh_object.attributes.each do |attr, orig_value|
#changed_attributes[attr] = orig_value if _field_changed?(attr, orig_value, #attributes[attr])
end
self
end
#changed_attributes is ActiveModel::Dirty's hash to store changed values. We obviously have to reset it.
fresh_object is the same record, freshly fetched from the database (this line comes from reload, thanks emaillenin).
Within the loop, each (fresh) attribute is compared to the corresponding (in-memory) attribute. If they differ, it is added to the #changed_attributes hash. This line comes from ActiveRecord's dup method. (it's actually from a private method called by dup, and _field_changed is private, too. It might be better to use ActiveRecord's public API, but I was lazy)
Finally, refresh returns self for convenience, just like reload does.
Here's an example usage:
p1 = Product.first
p1.name #=> "some name"
p1.changed? #=> false
p2 = Product.first
p2.name = "other name"
p2.save #=> true
p1.refresh
p1.name #=> "some name"
p1.changed? #=> true
p1.name_changed? #=> true
p1.name_was #=> "other name"
p1.name = "other name"
p1.name_changed? #=> false
p1.changed? #=> true
p1.changes #=> {"updated_at"=> [Tue, 29 Jul 2014 21:58:57 CEST +02:00, Tue, 29 Jul 2014 15:49:54 CEST +02:00]}
def database_changed?
self.class.where(self.class.arel_table[:updated_at].gt(updated_at)).exists? self
end
The Rails way to to do this is to use reload method on the ActiveRecord object.
def database_changed?
attributes != reload.attributes
end
Terminal 1
2.1.2 :001 > c = Car.find(1)
Car Load (0.4ms) SELECT "cars".* FROM "cars" WHERE "cars"."id" = ? LIMIT 1 [["id", 1]]
=> #<Car id: 1, name: "Audi", model: "R8", created_at: "2014-07-29 11:14:43", updated_at: "2014-07-29 11:14:43">
2.1.2 :002 > c.database_changed?
Car Load (0.1ms) SELECT "cars".* FROM "cars" WHERE "cars"."id" = ? LIMIT 1 [["id", 1]]
=> false
2.1.2 :003 > c.database_changed?
Car Load (0.2ms) SELECT "cars".* FROM "cars" WHERE "cars"."id" = ? LIMIT 1 [["id", 1]]
=> false
2.1.2 :004 > c.database_changed?
Car Load (0.2ms) SELECT "cars".* FROM "cars" WHERE "cars"."id" = ? LIMIT 1 [["id", 1]]
=> true
Terminal 2
2.1.2 :001 > c = Car.find(1)
Car Load (0.2ms) SELECT "cars".* FROM "cars" WHERE "cars"."id" = ? LIMIT 1 [["id", 1]]
=> #<Car id: 1, name: "Audi", model: "R8", created_at: "2014-07-29 11:14:43", updated_at: "2014-07-29 11:14:43">
2.1.2 :002 > c.model = 'A5'
=> "A5"
2.1.2 :003 > c.save!
(0.2ms) begin transaction
SQL (0.3ms) UPDATE "cars" SET "model" = ?, "updated_at" = ? WHERE "cars"."id" = 1 [["model", "A5"], ["updated_at", "2014-07-29 11:15:32.845875"]]
(1.2ms) commit transaction
=> true
2.1.2 :004 >

Get the Next/Previous record in ActiveRecord on Ruby on Rails

I am trying to get the next/previous record by ActiveRecord. The records should be retrieved according to the order by 'updated_at' column.
The name of the Model is 'Youtube'. And as the following console, this code couldn't get the right record and I guess the idea of my code seems bad because updated_at is not always unique so some records might have the same time stamp.
How do you get the next/previous record in a right way?
Console said below.
[57] pry(main)> Youtube.find(1000)
Youtube Load (0.5ms) SELECT "youtubes".* FROM "youtubes" WHERE "youtubes"."id" = $1 ORDER BY updated_at DESC LIMIT 1 [["id", 1000]]
=> #<Youtube id: 1000, author_id: 2, category_label: nil, generated_by: 1, title: "Is Kenya Mall Shooting Over? Were Americans Among A...", video_id: "4T1szQIQcNI", created_at: "2013-09-30 18:31:21", updated_at: "2013-10-27 02:19:56", subtitles: nil>
[58] pry(main)> Youtube.find(1000).next
Youtube Load (0.6ms) SELECT "youtubes".* FROM "youtubes" WHERE "youtubes"."id" = $1 ORDER BY updated_at DESC LIMIT 1 [["id", 1000]]
Sun, 27 Oct 2013 02:19:56 UTC +00:00
Youtube Load (256.6ms) SELECT "youtubes".* FROM "youtubes" WHERE (updated_at > '2013-10-27 02:19:56.593969') ORDER BY updated_at DESC LIMIT 1
=> #<Youtube id: 67003, author_id: 75, category_label: nil, generated_by: 1, title: "Jewelry Photography : Lenses for Jewelry Photograph...", video_id: "NqA7OZL4tzw", created_at: "2013-10-09 17:18:53", updated_at: "2013-10-28 02:17:33", subtitles: nil>
[59] pry(main)> Youtube.find(1000).previous
Youtube Load (0.6ms) SELECT "youtubes".* FROM "youtubes" WHERE "youtubes"."id" = $1 ORDER BY updated_at DESC LIMIT 1 [["id", 1000]]
Sun, 27 Oct 2013 02:19:56 UTC +00:00
Youtube Load (56.3ms) SELECT "youtubes".* FROM "youtubes" WHERE (updated_at < '2013-10-27 02:19:56.593969') ORDER BY updated_at DESC LIMIT 1
=> #<Youtube id: 999, author_id: 8, category_label: nil, generated_by: 1, title: "Authors#Google: Richard Moore, Ned Boulting, and Da...", video_id: "4SCzfuJAyJw", created_at: "2013-09-30 18:31:21", updated_at: "2013-10-27 02:19:55", subtitles: nil>
Youtube has the following default_scope. Although this might be changed according to some situation, but I hope this code would be kept to keep the existing behaviour.
default_scope order('updated_at DESC')
My trial code for Youtube Model is below.
scope :next, lambda{|updated_at| where("updated_at > ?",
updated_at).order("updated_at DESC")}
scope :previous, lambda {|updated_at| where("updated_at < ?",
updated_at).order("updated_at DESC")}
...
def next
self.class.next(updated_at).first
end
def previous
self.class.previous(updated_at).first
end
I did try and error and found below is one of the solutions.
code is here.
def next
self.class.unscoped.where("updated_at <= ? AND id != ?", updated_at, id).order("updated_at DESC").first
end
def previous
self.class.unscoped.where("updated_at >= ? AND id != ?", updated_at, id).order("updated_at ASC").first
end
test is here.
[210] pry(main)> Youtube.find(100)
Youtube Load (0.8ms) SELECT "youtubes".* FROM "youtubes" WHERE "youtubes"."id" = $1 ORDER BY updated_at DESC LIMIT 1 [["id", 100]]
=> #<Youtube id: 100, author_id: 5, category_label: nil, generated_by: 1, title: "Woman's Profane Dunkin Donuts Rant Goes Viral", video_id: "-aqN7KdWgQE", created_at: "2013-09-30 18:19:42", updated_at: "2013-10-27 00:47:37", subtitles: nil>
[211] pry(main)> Youtube.find(100).next
Youtube Load (0.7ms) SELECT "youtubes".* FROM "youtubes" WHERE "youtubes"."id" = $1 ORDER BY updated_at DESC LIMIT 1 [["id", 100]]
Youtube Load (95.9ms) SELECT "youtubes".* FROM "youtubes" WHERE (updated_at <= '2013-10-27 00:47:37.241076' AND id != 100) ORDER BY updated_at DESC LIMIT 1
=> #<Youtube id: 99, author_id: 6, category_label: nil, generated_by: 1, title: "Editing physical locations in Google Maps", video_id: "-amPC4fcY0U", created_at: "2013-09-30 18:19:42", updated_at: "2013-10-27 00:47:36", subtitles: nil>
[212] pry(main)> Youtube.find(100).next.previous
Youtube Load (0.7ms) SELECT "youtubes".* FROM "youtubes" WHERE "youtubes"."id" = $1 ORDER BY updated_at DESC LIMIT 1 [["id", 100]]
Youtube Load (68.8ms) SELECT "youtubes".* FROM "youtubes" WHERE (updated_at <= '2013-10-27 00:47:37.241076' AND id != 100) ORDER BY updated_at DESC LIMIT 1
Youtube Load (79.5ms) SELECT "youtubes".* FROM "youtubes" WHERE (updated_at >= '2013-10-27 00:47:36.162671' AND id != 99) ORDER BY updated_at ASC LIMIT 1
=> #<Youtube id: 100, author_id: 5, category_label: nil, generated_by: 1, title: "Woman's Profane Dunkin Donuts Rant Goes Viral", video_id: "-aqN7KdWgQE", created_at: "2013-09-30 18:19:42", updated_at: "2013-10-27 00:47:37", subtitles: nil>
[213] pry(main)> Youtube.find(100) === Youtube.find(100).next.previous
Youtube Load (0.8ms) SELECT "youtubes".* FROM "youtubes" WHERE "youtubes"."id" = $1 ORDER BY updated_at DESC LIMIT 1 [["id", 100]]
Youtube Load (4.8ms) SELECT "youtubes".* FROM "youtubes" WHERE "youtubes"."id" = $1 ORDER BY updated_at DESC LIMIT 1 [["id", 100]]
Youtube Load (99.7ms) SELECT "youtubes".* FROM "youtubes" WHERE (updated_at <= '2013-10-27 00:47:37.241076' AND id != 100) ORDER BY updated_at DESC LIMIT 1
Youtube Load (79.6ms) SELECT "youtubes".* FROM "youtubes" WHERE (updated_at >= '2013-10-27 00:47:36.162671' AND id != 99) ORDER BY updated_at ASC LIMIT 1
=> true
I wrote a gem that generates such queries automatically, order_query:
class YouTube < ActiveRecord::Base
include OrderQuery
order_query :order_display, [
[:updated_at, :desc],
[:id, :desc]
]
end
video = YouTube.find(42)
pos = video.order_display
video.next
video.after
video.position
video.prev
video.before
I would do it like this (might be better ways)...
scope :in_order, lambda{ order("updated_at DESC, id ASC") }
def next
self.class.in_order.where("updated_at >= ? AND id != ?", updated_at, id).first
end
def previous
self.class.in_order.where("updated_at <= ? AND id != ?", updated_at, id).first
end

Resources