activerecord

Rails ActiveRecord How Can I turn off logging particular queries

会有一股神秘感。 提交于 2019-12-11 03:19:52
问题 I am under developer mode in Rails. I am trying to insert a record with binary data to DB using AR. Edoc.create( :id_claim => @claim_index, :id_material => @doc_code, :size => @file_list.first[:size], :efile => params[:files][0].read ) But It takes a lot of time because Rails logs that query with entire file content. How Can I turn off logging particular queries? 回答1: Try: old_logger = ActiveRecord::Base.logger ActiveRecord::Base.logger = nil # your code here... Restore logger: ActiveRecord:

How to override the attr_protected?

二次信任 提交于 2019-12-11 03:18:26
问题 I have STI implementation as follows: class Automobile < ActiveRecord::Base end class Car < Automobile end class Truck < Automobile end class User < ActiveRecord::Base has_many :automobiles accepts_nested_attributes_for :automobiles end I am creating a list of automobiles for a user. For each automobile, the UI sets the type field and the properties associated with the automobile.While form submission, the type field is ignored as it is a protected attribute. How do I work around this issue?

Activerecord-import & serial column in PostgreSQL

十年热恋 提交于 2019-12-11 03:16:36
问题 I am in the process of upgrading a Rails 2.3.4 project to Rails 3.1.1. The old version used ar-extensions to handle a data import. I pulled out ar-extensions and replaced it with activerecord-import, which I understand has exactly the same interfaces. My code calls looks like this Student.import(columns, values) Both args are valid arrays holding the correct data, but I get a big fat error! The error stack looks like this: NoMethodError (You have a nil object when you didn't expect it! You

FactoryBot namespaced models without class_name

蓝咒 提交于 2019-12-11 03:09:44
问题 I have models which are namespaced such as this: class Vehicle < ActiveRecord::Base; end class Vehicle::Car < Vehicle; end class Vehicle::Train < Vehicle; end class Vehicle::Jet < Vehicle; end When creating factories for these models, they were set up in the following way: factory :vehicle_car, class: Vehicle::Car do; end factory :vehicle_train, class: Vehicle::Train do; end factory :vehicle_jet, class: Vehicle::Jet do; end This produces the following deprecation warning: DEPRECATION WARNING:

Delete associated records when model is saved

ⅰ亾dé卋堺 提交于 2019-12-11 03:06:27
问题 I am running ActiveRecord 3.2.6. Given I have these model definitions: My Invoice model class Invoice < ActiveRecord::Base has_many :items, :autosave => true, :dependent => :delete_all attr_accessible :recipient_email # This is just a simple wrapper with allows me to build multiple # items at once and to specify them as a Hash instead of Item.new. def items=(ary) super ary.map{|item| item.is_a?(Hash) ? items.build(item) : item} end end My Item model class Item < ActiveRecord::Base belongs_to

Handling an ActiveRecord error if database is empty

夙愿已清 提交于 2019-12-11 02:55:38
问题 I'm working on a rails 4 app, and i have the following controller code def index @issue = Issue.find(1) @sections = @issue.sections @articles = @issue.articles end which breaks if the database is empty with the error: "Couldn't find Issue with id=1". What is the proper way to check for this in a way that if nothing is in the db it doesn't raise an error? 回答1: One method you can use is the exists? active record method, like so: @issue = Issue.where(id: 1) if @issue.exists? # do something if it

Override rails update_all method

大城市里の小女人 提交于 2019-12-11 02:47:42
问题 I need to override the rails (active record) update_all method so that it always updates the updated_at field as well. How should I go about achieving this? 回答1: Put the following code in a file /config/initializers/update_all_with_touch.rb class ActiveRecord::Relation def update_all_with_touch(updates, conditions = nil, options = {}) now = Time.now # Inject the 'updated_at' column into the updates case updates when Hash; updates.merge!(updated_at: now) when String; updates += ", updated_at =

Rails is not saving an attribute that is changed [duplicate]

若如初见. 提交于 2019-12-11 02:40:01
问题 This question already has answers here : New data not persisting to Rails array column on Postgres (3 answers) Closed 4 years ago . I am appending some text to a notes field on one of my ActiveRecord::Base models but when I save it, it doesn't get updated: valve.notes #=> "Level: Top" valve.notes << "\nDirection: North" valve.notes #=> "Level: Top\nDirection: North" valve.save #=> true valve.reload.notes #=> "Level: Top" 回答1: Concat doesn't tell ActiveRecord that an Attribute has changed.

Cleaning up view ruby logic and separating concerns into model/controller

笑着哭i 提交于 2019-12-11 02:38:39
问题 I want to display a random assortment of 6 tools from my database on my home page. I have created a Pages controller with a home action. This is my Pages controller: class PagesController < ApplicationController def home @tools = Tool.all end end Then in my home.html.erb view I use the .sample method to grab random tools from my database as such(I repeat this 6 times using tool1, tool2, tool3, etc variables for each): <% tool1 = @tools.sample %> <%= image_tag tool1.tool_image.url(:medium) %>

Rails and Postgres aren't getting along after switching from Macports to Homebrew; PGError: ERROR: unrecognized time zone name: “UTC”

隐身守侯 提交于 2019-12-11 02:38:07
问题 I moved everything that I had in /opt/local (where Macports' things lived) into ~/.macports . That included my old postgres database cluster. Then I installed postgres using Homebrew and ran the setup that it told me to (creating a new database cluster in /usr/local ). In my project, I ran rake db:drop:all and rake db:create . These did their work cheerfully. But when I tried to rake db:migrate , postgres and rails fight and tell me rake aborted! PGError: ERROR: unrecognized time zone name: