activerecord

What Does This Rails4 error mean? fatal: exception reentered … `rescue in rollback_active_record_state!'

ⅰ亾dé卋堺 提交于 2019-12-08 14:47:17
问题 I'm getting a couple of errors in my Rails 4 appointment scheduling application that I can't seem to correct or figure out the root cause. My seeds file keeps breaking with well known "error, stack level too deep". But when I run the method I believe it is breaking on, I get this different error: Seeding time slots for workday no. 1 (0.5ms) SAVEPOINT active_record_1 (0.5ms) ROLLBACK TO SAVEPOINT active_record_1 fatal: exception reentered from /Users/rskelley/.rvm/gems/ruby-2.0.0-p481/gems

How to make Active Record join return unique objects?

拜拜、爱过 提交于 2019-12-08 14:30:25
问题 I have a simple query need: Find a list of users who made an order since Jan 1, 2013. In SQL, it's a very simple query. But I'm using Rails and Active Record. So I wrote: User.joins(:orders).where("orders.created_at >= '2013-01-01 00:00:00'") In our database, we have 100 orders made since 01/01/2013 by 75 users. (Some users made more than one order apparently.) However, the expression above returns 100 users. (There must be duplicates.) I tried User.joins(:orders).where("orders.created_at >=

Rails 4.1.0 No Method error when trying to update a parent table from a child table

旧时模样 提交于 2019-12-08 13:57:50
问题 I have 2 rails models, a security and stock_quote model, which are as follows class StockQuote < ActiveRecord::Base belongs_to :security, class_name: "Security", foreign_key: 'security_id' def self.price_on(date) where("date(created_at) = ?", date).sum(:high) end feed_url = "https://spreadsheets.google.com/feeds/list/1ncyK8uXoeLobVkdiSKQcYJr2joK_uN5QSBB3814GKaw/od6/public/values" def self.update_from_feed(feed_url) feed = Feedjira::Feed.fetch_and_parse(feed_url) unless feed.is_a?(Fixnum) add

how to add index conditionally

人走茶凉 提交于 2019-12-08 13:55:51
问题 say i have a model class Post < ActiveRecord::Base validates_uniqueness_of :title, :unless => Proc.new {|p| p.deleted?} end The constraint is i can have only 1 post having "foobar" as its title while it's not deleted, and 1+ posts, which are all having deleted to be true, also having "foobar" as their titles. Since ActiveRecord can not guarantee the uniqueness of the title from this link, I'm trying to add a unique index to the table posts, on columns [:title, :deleted], it will fail the

Rails 3.2 (ActiveRecord) association order with eager loading and ordering

孤街浪徒 提交于 2019-12-08 13:51:06
问题 I came upon this behavior in rails that I can't seem to find in the documentation. It appears that if you complicate an ActiveRecord query to the point that it lumps the underlying SQL into a "complicated" query (see below) it doesn't honor ordering specified on the associations. Here's my example model: class Article < ActiveRecord::Base belongs_to :user has_many :categories, :order => :name attr_accessible :title, :user_id, :user end If I perform a "simple" query, I can get the associated

Why doesn't accepts_nested_attributes_for allow_destroy not work?

最后都变了- 提交于 2019-12-08 13:48:43
问题 I have a nested attribute/resource, that I'm trying to destroy. What I want to do is to destroy a review object, when an associated life_hack article object is destroyed. Now I'm not too sure how the accepted_as_nested_attributes work but I'm pretty sure this is the way that I need to do it. Below i have some routes defined as so: Hacklife::Application.routes.draw do devise_for :users root 'life_hacks#index' resources :life_hacks, only: [:new, :index, :create, :show, :destroy] do resources

Requiring ActiveRecord on IRB - Ruby (NO Rails)

99封情书 提交于 2019-12-08 13:27:30
How can I load ActiveRecord on an IRB session? I have the following # config/app.rb require 'active_record' ActiveRecord::Base.establish_connection( adapter: 'sqlite3', database: 'db/mydb.sqlite3' ) But when I start IRB and try to load it irb#1(main):001:0> require config/application.rb I get NameError: undefined local variable or method `config' for main:Object Did you mean? conf I'd like to be able to interact with my ActiveRecord objects from IRB. I'm NOT using Rails but only ActiveRecord. Thanks Two things to change here: Always put quotes around the path you're requiring. The reason Ruby

Is there a way to pass params to postgres raw query in rails?

走远了吗. 提交于 2019-12-08 13:25:24
问题 I have a complex raw query where I want to pass params instead of interpolating them Ex: query = "SELECT id, abbr as code, name FROM states WHERE name IN ('#{params[:state].join("','")}')" ActiveRecord::Base.connection.execute(query) Is there any way to pass params like the ActiveRecord does State.where(name: ['Alabama', 'Alaska']) 回答1: Try find_by_sql: Post.find_by_sql(["SELECT title FROM posts WHERE author = ? AND created > ?", author_id, start_date]) [#<Post:0x36bff9c @attributes={"title"=

Rails 5 create multiple child objects using parent object by the use of nested attributes

混江龙づ霸主 提交于 2019-12-08 12:39:08
问题 So, I'm fairly new to Rails and I'm stuck due to my model's complexity. I have a Developer model, a Township model and a Project model and their contents are as follows:- Developer.rb Class Developer < ApplicationRecord has_many :townships, has_many :projects, through: :townships accepts_nested_attributes_for :township end Township.rb Class Township < ApplicationRecord belongs_to :developer has_many :projects accepts_nested_attributes_for :project end Project.rb Class Project <

Codeigniter Active records complex query

帅比萌擦擦* 提交于 2019-12-08 12:32:49
问题 > * 1. I need to write this in Active records * i need to join these 3 tables , but the condition for join is very very selective $this->db->select(name); $this->db->from('table0'); $this->db->join('table1','(table1.id=0 AND table0.feild1 = table1.feild1) OR (table1.id=1 AND table0.feild2 = table1.feild2)') // <--- how to write this is my question i could do a simple join but the main problem is achieving the condition in the join that i have mentioned above.Also this is a small part of a