activerecord

Update multiple records simultaneously with ActiveRecord in Rails using one query?

情到浓时终转凉″ 提交于 2019-12-31 21:59:19
问题 Let's suppose I have a table called 'user_products' and a corresponding model called UserProduct in my Rails application. I also have a field called 'is_temporary' in my table. Now suppose I want to run a query like this but using the ActiveRecord abstraction layer: UPDATE user_products SET is_temporary = false WHERE user_id = 12345; Is there a way I can do this using ActiveRecord? Maybe something along the lines of UserProduct.find_by_user_id(12345).update_attributes(:is_temporary => false)

What is the fastest way to create mass HABTM associations in Rails?

霸气de小男生 提交于 2019-12-31 21:43:12
问题 I have two tables, with a HABTM relationship in Rails. Something like the following: class Foo &lt ActiveRecord::Base has_and_belongs_to_many :bars end class Bar &lt ActiveRecord::Base has_and_belongs_to_many :foos end Now I have a new Foo object, and want to mass-assign thousands of bars to it, which I've pre-loaded: @foo = Foo.create @bars = Bar.find_all_by_some_attribute(:a) What's the fastest way to do this? I've tried: @foo.bars = @bars @foo.bars &lt&lt @bars And both run really slow,

Rails - Paperclip validating attachment size when it shouldn't be?

寵の児 提交于 2019-12-31 21:42:12
问题 I've got a rails model using Paperclip that looks like this: has_attached_file :image, :styles => { :normal => ['857x392#', :png] }, :url => '/assets/pages/:id/:basename.:extension', :path => ':rails_root/public/assets/pages/:id/:basename.:extension' validates_attachment_size :image, :less_than => 2.megabytes When attempting to create a record of this model without an attachment to upload, the validation error is returned: There were problems with the following fields: * Image file size file

Rails: is it possible to add extra attribute to a has_and_belongs_to_many association?

偶尔善良 提交于 2019-12-31 12:18:30
问题 What I mean is if I have two models, connected by a has_and_belongs_to_many association, can I store other data in the join table for each association? That is, the extra data would not be part of a single record in either table, but instead of the connection between them. My actual models are as follows: class Part < ActiveRecord::Base has_and_belongs_to_many :assemblies has_and_belongs_to_many :packages belongs_to :user validates :name, :user_id, :presence => true end class Package <

Loop through ActiveRecord::Associations::CollectionProxy with each

非 Y 不嫁゛ 提交于 2019-12-31 10:49:34
问题 I have the following models set up with active record 4 (mapping a legacy database) class Page < ActiveRecord::Base self.table_name = "page" self.primary_key = "page_id" has_many :content, foreign_key: 'content_page_id', class_name: 'PageContent' end class PageContent < ActiveRecord::Base self.table_name = "page_content" self.primary_key = "content_id" belongs_to :pages, foreign_key: 'page_id', class_name: 'Page' end The following works fine.... page = Page.first page.content.first.content_id

How to get the line of code that triggers a query?

这一生的挚爱 提交于 2019-12-31 10:24:14
问题 is there a way (a gem, a plugin or something else) in rails 3.2 to know which line of code triggers a database query? For example in my log I have: User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 5 LIMIT 1 How can I know the line of code that triggers the query? Thx... 回答1: I've found this solution: module QueryTrace def self.enable! ::ActiveRecord::LogSubscriber.send(:include, self) end def self.append_features(klass) super klass.class_eval do unless method_defined?(:log

How to get the line of code that triggers a query?

拜拜、爱过 提交于 2019-12-31 10:23:15
问题 is there a way (a gem, a plugin or something else) in rails 3.2 to know which line of code triggers a database query? For example in my log I have: User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 5 LIMIT 1 How can I know the line of code that triggers the query? Thx... 回答1: I've found this solution: module QueryTrace def self.enable! ::ActiveRecord::LogSubscriber.send(:include, self) end def self.append_features(klass) super klass.class_eval do unless method_defined?(:log

How to get the line of code that triggers a query?

对着背影说爱祢 提交于 2019-12-31 10:23:01
问题 is there a way (a gem, a plugin or something else) in rails 3.2 to know which line of code triggers a database query? For example in my log I have: User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 5 LIMIT 1 How can I know the line of code that triggers the query? Thx... 回答1: I've found this solution: module QueryTrace def self.enable! ::ActiveRecord::LogSubscriber.send(:include, self) end def self.append_features(klass) super klass.class_eval do unless method_defined?(:log

How can I use Mongoid and ActiveRecord in parallel in Rails 3?

不想你离开。 提交于 2019-12-31 09:17:09
问题 I'm using rails 3, and began my application with ActiveRecord. Now, I have many models, and the relations are starting to get complicated, and some could be more simply expressed with a Document-Oriented structure, so I'd like to try migrating to MongoDB and use Mongoid. I've always heard that you didn't have to eitheer use all MongoDB or nothing, but that you could use the two in parallel while migrating. I don't see how to go about this from the docs though. For example, I have: class User

Using ActiveRecord, is there a way to get the old values of a record during after_update

柔情痞子 提交于 2019-12-31 08:08:08
问题 Setup using a simple example: I've got 1 table ( Totals ) that holds the sum of the amount column of each record in a second table ( Things ). When a thing.amount gets updated, I'd like to simply add the difference between the old value and the new value to total.sum . Right now I'm subtracting self.amount during before_update and adding self.amount during after_update . This places WAY too much trust in the update succeeding. Constraint: I don't want to simply recalculate the sum of all the