activerecord

PostgreSQL and ActiveRecord subselect for race condition

妖精的绣舞 提交于 2020-01-05 01:09:10
问题 I'm experiencing a race condition in ActiveRecord with PostgreSQL where I'm reading a value then incrementing it and inserting a new record: num = Foo.where(bar_id: 42).maximum(:number) Foo.create!({ bar_id: 42, number: num + 1 }) At scale, multiple threads will simultaneously read then write the same value of number . Wrapping this in a transaction doesn't fix the race condition because the SELECT doesn't lock the table. I can't use an auto increment, because number is not unique, it's only

PostgreSQL and ActiveRecord subselect for race condition

只愿长相守 提交于 2020-01-05 01:09:02
问题 I'm experiencing a race condition in ActiveRecord with PostgreSQL where I'm reading a value then incrementing it and inserting a new record: num = Foo.where(bar_id: 42).maximum(:number) Foo.create!({ bar_id: 42, number: num + 1 }) At scale, multiple threads will simultaneously read then write the same value of number . Wrapping this in a transaction doesn't fix the race condition because the SELECT doesn't lock the table. I can't use an auto increment, because number is not unique, it's only

Requiring Active_record 4.1.8 gives warnings

别来无恙 提交于 2020-01-04 16:58:04
问题 I upgraded to 4.1.8 and now when I open a console session and do require "active_record" I get the following output: [1] pry(main)> require 'active_record' /Users/fzondlo/.rvm/gems/ruby-2.0.0-p195/gems/bigdecimal-1.2.5/bigdecimal.bundle: warning: already initialized constant BigDecimal::BASE /Users/fzondlo/.rvm/gems/ruby-2.0.0-p195/gems/bigdecimal-1.2.5/bigdecimal.bundle: warning: already initialized constant BigDecimal::EXCEPTION_ALL /Users/fzondlo/.rvm/gems/ruby-2.0.0-p195/gems/bigdecimal-1

Weird issue has_many through association in updated Rails

做~自己de王妃 提交于 2020-01-04 15:17:09
问题 I updated from Ruby 2.1.2 to Ruby 2.2.0 and updated all the gems linked to it. I've a few models linked between each others like class Question < ActiveRecord::Base belongs_to :course_version has_one :course, through: :course_version has_many :answer_questions has_many :answers, through: :answer_questions end class AnswerQuestion < ActiveRecord::Base belongs_to :answer belongs_to :question end class Answer < ActiveRecord::Base has_many :answer_questions has_many :questions, through: :answer

Is overriding an ActiveRecord relation's count() method okay?

核能气质少年 提交于 2020-01-04 14:15:03
问题 Let's say I have the following relationship in my Rails app: class Parent < ActiveRecord::Base has_many :kids end class Kid < ActiveRecord::Base belongs_to :parent end I want parents to be able to see a list of their chatty kids, and use the count in paginating through that list. Here's a way to do that (I know it's a little odd, bear with me): class Parent < ActiveRecord::Base has_many :kids do def for_chatting proxy_association.owner.kids.where(:chatty => true) end end end But! Some parents

Rails 3 ActiveRecord where clause where id is set or null

∥☆過路亽.° 提交于 2020-01-04 10:42:52
问题 I have an ActiveRecord, Annotation, with two columns: user_id and post_id, which can be null. An annotation with null user_id and post_id are "global" annotations that are applicable to all posts across all users. I would like to retrieve all annotations associated with a particular user's post AND all global annotations. In MySQL, the SQL command would be select * from annotations where (user_id = 123 and post_id = 456) or (user_id is null and post_id is null) In Rails 3, what is best way to

Rails 3 ActiveRecord where clause where id is set or null

偶尔善良 提交于 2020-01-04 10:42:16
问题 I have an ActiveRecord, Annotation, with two columns: user_id and post_id, which can be null. An annotation with null user_id and post_id are "global" annotations that are applicable to all posts across all users. I would like to retrieve all annotations associated with a particular user's post AND all global annotations. In MySQL, the SQL command would be select * from annotations where (user_id = 123 and post_id = 456) or (user_id is null and post_id is null) In Rails 3, what is best way to

Rails 3 ActiveRecord where clause where id is set or null

风流意气都作罢 提交于 2020-01-04 10:42:01
问题 I have an ActiveRecord, Annotation, with two columns: user_id and post_id, which can be null. An annotation with null user_id and post_id are "global" annotations that are applicable to all posts across all users. I would like to retrieve all annotations associated with a particular user's post AND all global annotations. In MySQL, the SQL command would be select * from annotations where (user_id = 123 and post_id = 456) or (user_id is null and post_id is null) In Rails 3, what is best way to

ActiveRecord: keep duplicated objects in .where query

一笑奈何 提交于 2020-01-04 10:24:53
问题 I have an array of ids: ids = [1, 2, 3, 1] If I run: Product.where(id: ids) I will only get one occurence of the Product having the iD 1 . However, I'd like to get as many occurences of the object as there are in the array. How can I do this ? 回答1: You could load the unique records and then transform the result into an Array with multiple references to each book: ids = [2109, 2511, 2108, 2109] tmp_books = Book.find(ids) new_books = ids.map {|x| tmp_books.detect {|b| b.id == x } } That

ActiveRecord: keep duplicated objects in .where query

女生的网名这么多〃 提交于 2020-01-04 10:22:22
问题 I have an array of ids: ids = [1, 2, 3, 1] If I run: Product.where(id: ids) I will only get one occurence of the Product having the iD 1 . However, I'd like to get as many occurences of the object as there are in the array. How can I do this ? 回答1: You could load the unique records and then transform the result into an Array with multiple references to each book: ids = [2109, 2511, 2108, 2109] tmp_books = Book.find(ids) new_books = ids.map {|x| tmp_books.detect {|b| b.id == x } } That