activerecord

My custom destroy method does not trigger the default before and after destroy callbacks

旧城冷巷雨未停 提交于 2019-12-12 01:32:59
问题 I am writing a plugin that provides drafting for models. A delete action is a draftable action and I do not always want to delete the origin until that deletion is published. So I wrote my own destroy method to help out with this. Everything works exactly as I want things to except, custom callbacks for :before_destroy and :after_destroy are no longer being triggered. Any ideas on how to: rebind callbacks to my destroy method works some alias_method_chain voodoo get a list of model callbacks

Debugging why a create method is not saving to the model

ε祈祈猫儿з 提交于 2019-12-12 01:15:13
问题 I have this method which after a rake task should save my data to the model, I am trying to update 2 columns at a time, which shouldnt be a problem def update_fixtures #rake task method Fixture.destroy_all get_home_fixtures.each {|home| Fixture.create!(home_team: home )} get_away_fixtures.each {|away| Fixture.create!(away_team: away )} end In this instance only the away team will save, however if i do this def update_fixtures #rake task method Fixture.destroy_all get_away_fixtures.each {|away

Calculate Percent of total from scoped records and display virtual attributes (Rails / Active Record)

痴心易碎 提交于 2019-12-12 01:11:34
问题 Create a scope, maybe something like this.. scope :mv, select('*,quantity*market_price as market_value, quantity*market_price/sum(quantity*market_price) as percent') that creates two virtual attributes, market_value and percent. The problem I am having is creating the percent with the sum() included. If I add sum(), the scope returns one record. I need to calculate the percent of market_value relative to the total market value of the scoped record set. example: 1, market_value: 100, percent:

How to validate that payment can never cause invoice amount payable to be less than zero?

有些话、适合烂在心里 提交于 2019-12-12 01:04:26
问题 I have this class: class Payment < ActiveRecord::Base attr_accessible :amount, :invoice_id belongs_to :invoice validates :amount, :numericality => { :greater_than => 0, :less_than_or_equal_to => :maximum_amount } after_save :update_amount_payable after_destroy :update_amount_payable private def maximum_amount invoice.amount_payable end def update_amount_payable invoice.update_amount_payable end end class Invoice < ActiveRecord::Base has_many :payments after_save :update_amount_payable def

Rails Migration - PG::Error: ERROR: invalid input syntax for integer: “”

南楼画角 提交于 2019-12-12 00:59:11
问题 I am trying to deploy my code to heroku and i get the error -- execute("ALTER TABLE lodges ALTER COLUMN image TYPE integer USING (image::integer)") PG::Error: ERROR: invalid input syntax for integer: "" : ALTER TABLE lodges ALTER COLUMN image TYPE integer USING (image::integer) rake aborted! and my migration is class ChangeDataTypeForLodgesImage < ActiveRecord::Migration def change execute "ALTER TABLE lodges ALTER COLUMN image TYPE integer USING (image::integer)" end end 回答1: That error is

Using ActiveRecord with column names with spaces in them

亡梦爱人 提交于 2019-12-12 00:56:15
问题 I work on a team that is using ActiveRecord to access the schema on a MSSQL server. Modifying the schema is not an option and the column names have spaces in them, a la SPACEY COLUMN . When writing the ActiveRecord class to access a table with spaces in it, what is good practice? We also need this to work with factory girl... 回答1: AR-JDBC (as well as the AR-SQLServer-Adapter) will/should handle this just fine since it auto-magically quotes column name identifiers using "[ COlumn NAME ]" ... I

Updating ActiveRecord in Rails test

白昼怎懂夜的黑 提交于 2019-12-11 23:59:56
问题 I'm writing my first Rails 'Store' app and I'm coming across something weird in one of my tests. I'm trying to test the add_product method on cart.rb: class Cart < ActiveRecord::Base has_many :line_items, :dependent => :destroy def add_product(product) current_item = line_items.find_by_product_id(product.id) if current_item current_item.quantity += 1 else current_item = line_items.build(:product_id => product.id, :price => product.price) end current_item end def total_price line_items.to_a

Rails 4 - Finding objects by join table condition

我们两清 提交于 2019-12-11 23:55:05
问题 I have two models, User and Team , it's a many-to-many with a join table called Member . It's set ut like this: #Team: has_many :members, dependent: :destroy has_many :users, through: :members #User has_many :members has_many :teams, through: :members #Member belongs_to :user belongs_to :team I want users to be able to visit each others profile pages ( controller: :users, action: :show ). On the profile page I only want to list the teams that both users are members of (they can be members of

Bug with ActiveRecords

自作多情 提交于 2019-12-11 23:27:50
问题 I can't delete information about tables and columns from ActiveRecord's cache. I'm using ActiveRecord for Ruby without Rails. require 'active_record' ActiveRecord::Base.establish_connection( :adapter => "mysql2", :database => # :password => # ) class Person < ActiveRecord::Base belongs_to :peoples end enter code here class Persons < ActiveRecord::Base belongs_to :peoples end class People < ActiveRecord::Base has_many :persons end Person.new ActiveRecord::StatementInvalid: Mysql2::Error: Table

Rails 3 — Build not saving to database (Railscast 196)

为君一笑 提交于 2019-12-11 23:25:51
问题 I'm following along with railscast 196. I've got two levels of associations. App -> Form -> Question. This is the new action in the form controller. def new @app = App.find(params[:app_id]) @form = Form.new 3.times {@form.questions.build } end the view is displaying all 3 questions fine and I can submit the form... but nothing is inserted into the database for the questions. Here is my create action def create @app = App.find(params[:app_id]) @form = @app.forms.create(params[:form]) respond