arel

undefined method on ActiveRecord::Relation object

自古美人都是妖i 提交于 2019-12-24 07:25:47
问题 the following code fieldvalue = Admin::FieldValue.where(:item_id => @admin_item.id, :field_id => key) fieldvalue.update_attributes(:value => value) raise the following error NoMethodError (undefined method `update_attributes' for #<ActiveRecord::Relation:0x00000102dfc868>): why is it a Relation object and not a FieldValue object, what's the right way to manage this 回答1: fieldvalue should return only one value? fieldvalue = Admin::FieldValue.where(:item_id => @admin_item.id, :field_id => key)

Use SQL functions for insert/update in ActiveRecord

浪尽此生 提交于 2019-12-23 17:38:13
问题 I want to store IP addresses (v4 and v6) in my rails application. I have installed an extension to MySQL adds functions to convert ip strings to binary which will allow me to query by IP range easily. I can use unescaped sql statements for SELECT type queries, thats easy. The hard part is that I also need a way to overwrite the way the field is escaped for insert/update statements. This ActiveRecord statement new_ip = Ip.new new_ip.start = '1.2.3.4' new_ip.save Should generate the following

Arel: order by association count

本秂侑毒 提交于 2019-12-23 03:37:13
问题 Here is what I'm trying to do class Question has_many :votes end class Vote belongs_to :question end I want to find all questions ordered by the number of votes they have. I want to express this in Arel (in Rails 3) without using any counter caches. Is there any way of doing this ? Thanks. 回答1: Try this: Question.select("questions.*, a.vote_count AS vote_count"). joins("LEFT OUTER JOIN ( SELECT b.question_id, COUNT(b.id) AS vote_count FROM votes b GROUP BY b.question_id ) a ON a.question_id =

Writing “not in” sql query using AREL

本秂侑毒 提交于 2019-12-22 05:14:34
问题 I have a scope defined as follows: scope :ignore_unavailable, lambda { where([ "Item.id NOT IN (SELECT id FROM Cars WHERE Cars.status = 'NA'" ]) } Currently its using hardcoded tables names. How can I improve it using frameworks like Arel ? Will appreciate any help here. I am on Rails 3.2 回答1: If you are using rails 4, one way would be scope :ignore_unavailable, lambda { where.not(id: Car.where(:status => "NA").pluck(:id)) } For rails 3 scope :ignore_unavailable, lambda { where("id not in (?)

Writing “not in” sql query using AREL

自古美人都是妖i 提交于 2019-12-22 05:14:15
问题 I have a scope defined as follows: scope :ignore_unavailable, lambda { where([ "Item.id NOT IN (SELECT id FROM Cars WHERE Cars.status = 'NA'" ]) } Currently its using hardcoded tables names. How can I improve it using frameworks like Arel ? Will appreciate any help here. I am on Rails 3.2 回答1: If you are using rails 4, one way would be scope :ignore_unavailable, lambda { where.not(id: Car.where(:status => "NA").pluck(:id)) } For rails 3 scope :ignore_unavailable, lambda { where("id not in (?)

How to convert this raw query to Active record query interface or arel?

别等时光非礼了梦想. 提交于 2019-12-22 00:13:15
问题 I want to find courses which have at least 1 variant with variant_type = 1 and don't have any variant with variant_type = 2. So my query like: Course.where("id IN ( SELECT course_id FROM course_variants WHERE variant_type = 1 ) AND id NOT IN ( SELECT course_id FROM course_variants WHERE variant_type = 2 )") Additionally, a course has many course_variants . I'm using a raw query in where clause, I want to improve this using Active record interface or Arel, any solution for this? Thank you!

Simulating has_and_belongs_to_many nested through behavior in Rails 3

让人想犯罪 __ 提交于 2019-12-21 22:20:47
问题 So Rails doesn't have support for :through associations through a habtm relationship. There are plugins out there that will add this in for Rails 2.x, but I'm using Rails 3 / Edge, and only need the association for one particular model. So I thought I'd stump it out myself with the beauty that is Arel. First, the models: class CardSet < ActiveRecord::Base has_and_belongs_to_many :cards, :uniq => true end class Card < ActiveRecord::Base has_and_belongs_to_many :card_sets, :uniq => true has

Concatenate (glue) where conditions by OR or AND (Arel, Rails3)

 ̄綄美尐妖づ 提交于 2019-12-21 04:57:24
问题 I have several complex queries (using subqueries, etc...) and want to glue them together with OR or AND statement. For example: where1=table.where(...) where2=table.where(...) I would like something like where3=where1.or where2 Next example doesn't work for me: users.where(users[:name].eq('bob').or(users[:age].lt(25))) because of I have several where(..) queries and I want to concatenate them . In other words I have 3 methods: first return first where, second-second, third - OR concatenation.

How would you do this Rails sub-query using Squeel?

喜夏-厌秋 提交于 2019-12-20 20:03:54
问题 I want to restructure the query below using Squeel. I'd like to do this so that I can chain the operators in it and re-use the logic in the different parts of the query. User.find_by_sql("SELECT users.*, users.computed_metric, users.age_in_seconds, ( users.computed_metric / age_in_seconds) as compound_computed_metric from ( select users.*, (users.id *2 ) as computed_metric, (extract(epoch from now()) - extract(epoch from users.created_at) ) as age_in_seconds from users ) as users") The query

How would you do this Rails sub-query using Squeel?

时间秒杀一切 提交于 2019-12-20 20:03:27
问题 I want to restructure the query below using Squeel. I'd like to do this so that I can chain the operators in it and re-use the logic in the different parts of the query. User.find_by_sql("SELECT users.*, users.computed_metric, users.age_in_seconds, ( users.computed_metric / age_in_seconds) as compound_computed_metric from ( select users.*, (users.id *2 ) as computed_metric, (extract(epoch from now()) - extract(epoch from users.created_at) ) as age_in_seconds from users ) as users") The query