activerecord

Active Record Update All JSON Field

大兔子大兔子 提交于 2019-12-12 14:02:56
问题 So I have a model Item that has a huge postgresql JSON field called properties . Most of the time this field does not need to be queried on or changed, however we store price in this field. I'm currently writing a script that updates this price , but there's only a few unique prices and thousands of Items so in order to save time I have a list of Items for each unique price and I'm attempting to do an update all: Item.where(id: items).update_all("properties->>'price' = #{unique_price}") But

How to JOIN two columns with one serialized data?

こ雲淡風輕ζ 提交于 2019-12-12 13:24:35
问题 I'am trying to build an eshop with multicategory support for products, so whenever I select a specific categories from <select> , I serialize() them and save them into DB. So for now, I would like to output the JOINed tables with all db datas I need by trying to join two tables: CATEGORIES and ECOMMERCE_PRODUCTS in my app/models/ecommerce_model.php : $this->db ->select(' ecommerce_products.id, categories.id AS catid, categories.title AS categories, ecommerce_products.manid, ecommerce_products

Grouped aggregations with Yii STAT?

蹲街弑〆低调 提交于 2019-12-12 12:43:01
问题 I have a Yii STAT Relation that's defined to provide a grouped SUM result, however when I access the relation in my View , the only value is the latest single value rather than each value . For example, here's my relation: 'total_salaries_by_job' => array( self::STAT, 'Employee', 'department_id', 'select' => 'job_type_id, SUM(salary)', 'group'=>"job_type_id" ) This generates the following SQL: SELECT department_id AS c , job_type_id , SUM(salary) AS s FROM Employee AS t WHERE t.department_id

Rails Model Validation Conditional allow_nil?

核能气质少年 提交于 2019-12-12 12:42:13
问题 So I was wondering if we can have a conditional allow_nil option for a validation on a rails model. What I would like to do is to be able to allow_nil depending upon some logic(some other attribute) So I have a product model which can be saved as draft. and when being saved as draft the price can be nil, but when not a draft save the price should be numerical. how can I achieve this. The following doesn't seem to work. It works for draft case but allows nil even when status isn't draft. class

Rails 3 ORDER BY FIELD and last

青春壹個敷衍的年華 提交于 2019-12-12 12:25:54
问题 Hello I have an issue with Rails 3.2 and ordering. When wanting to order a collection by a field, when calling .last ActiveRecord behaves weirdly... >> User.order("FIELD(id, '1')") User Load (0.4ms) SELECT `users`.* FROM `users` ORDER BY FIELD(id, '1') => [] >> User.order("FIELD(id, '1')").first User Load (0.4ms) SELECT `users`.* FROM `users` ORDER BY FIELD(id, '1') LIMIT 1 => nil >> User.order("FIELD(id, '1')").last User Load (0.3ms) SELECT `users`.* FROM `users` ORDER BY FIELD(id DESC, '1')

order by foreign key in activerecord

本秂侑毒 提交于 2019-12-12 12:16:08
问题 I have a tables Foo and Bar . Foo has one Bar . When I query Foo , how can I order it by a date column in the Bar table? Thanks 回答1: Foo.find(:all,:joins=>:boo, :order=>'bars.created_at DESC' ) 回答2: Refer to the ActiveRecord Query Interface page: http://guides.rubyonrails.org/active_record_querying.html#joining-tables Note that sometimes a prefix is added to the table name so you may need to do something like: Foo.all(:joins => :bar, :order => Bar.table_name + '.created_at') 来源: https:/

ActiveRecord raising duplicate key error on simple create

我与影子孤独终老i 提交于 2019-12-12 12:10:34
问题 I am getting a uniqueness violation for a reason I do not understand. I have a table for tracking Prices of various Equity objects. I create or update a price for a given equity on a given date like this: p = Price.where(equity_id: eq.id, date: date).first_or_create When I run this, ActiveRecord is throwing this exception: ActiveRecord::RecordNotUnique Exception: PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "prices_pkey" DETAIL: Key (id)=(4273819) already exists.

Active record. Model.Find.last

China☆狼群 提交于 2019-12-12 12:02:11
问题 I have an ActiveRecord relationship between Trade and Execution . I can get Trade.executions #returns all exeuctions realated to the Trade If I do Trade.executions.last It seems seems to return the last execution record based on ID. Is this the correct way to retrieve the last execution record related to Trade based on ID? 回答1: No, that's not guaranteed to give you the Execution with the highest id . If you don't specify an explicit ordering then the records can come out of the database in

Rails ActiveRecord: legacy table without primary key shows nil for result?

雨燕双飞 提交于 2019-12-12 11:37:11
问题 I've got a Rails app that'll be sitting on top of a legacy database with some ugly tables that I'm having to deal with. One is a feature_attributes table related to features . Problem is that this feature_attributes table doesn't have a primary key. I wouldn't think that'd be a problem, but evidently it is. I've got my model name which is different from the table name, but I'm using set_table_name to specify the right one. class Feature < ActiveRecord::Base has_many :feature_attributes end

How do I retrieve a list of created IDs for bulk insert in Active Record?

≯℡__Kan透↙ 提交于 2019-12-12 11:25:47
问题 I have three models: class Coupon < ActiveRecord::Base belongs_to :event has_many :coupon_events, :dependent => :destroy has_many :events, :through => :coupon_events end class Event < ActiveRecord::Base belongs_to :event has_many :coupon_events, :dependent => :destroy has_many :coupons, :through => :coupon_events end class CouponEvent < ActiveRecord::Base belongs_to :coupon belongs_to :event end I read through a CSV file to create coupons and coupon_events. This is terribly inefficient since