squeel

How to dynamically generate association names?

时光毁灭记忆、已成空白 提交于 2020-01-14 04:25:08
问题 I am using Ruby on Rails 3.2.2 and the Squeel gem. I have following statements and I am trying to refactoring the my_squeel_query method in a Mixin module (since it is used by many of my models): # Note: 'article_comment_associations' and 'model_as_like_article_comment_associations' # refer to database table names. class Article < ActiveRecord::Base def my_squeel_query commenters. .where{ article_comment_associations.article_id.eq(my{self.id}) & ... } end end class ModelAsLikeArticle <

How to make a query in Postgres to group records by month they were created? (:created_at datetime column)

▼魔方 西西 提交于 2020-01-03 05:25:06
问题 I have this Ruby code: def visits_chart_data(domain) last_12_months.collect do |month| { created_at: month, count: domain.visits.created_on_month(month).count } end end def last_12_months (0..11).to_a.reverse.collect{ |month_offset| month_offset.months.ago.beginning_of_month } end CreatedOnMonth is just a scope: scope :created_on_month, -> (month = Time.now) { where{ (created_at >= month.beginning_of_month) & (created_at <= month.end_of_month) } } created_at is just a standard datetime

Squeel request and Heroku, Postgres: Error using GROUP BY and ORDER

牧云@^-^@ 提交于 2019-12-24 09:27:03
问题 I am developing my application with mysql but I am using Heroku to deploy it and am forced to use PG. I have an issue with the following statement: <% current_user_savings = Saving.where{user_id == my{current_user}} %> <% @latest_savings = Saving.where{product_id.not_in(current_user_savings.select{product_id})}.group{product_id} %> So it work on my computer but when deploying to heroku I have the following issue : ActionView::Template::Error (PGError: ERROR: column "savings.id" must appear in

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

Ranking results with complex conditions using Rails and Squeel

送分小仙女□ 提交于 2019-12-20 06:41:54
问题 I'm probably doing something silly here - and I'm open to other ways of doing - but I'm trying to order my results set based on a computed field: Client.select{['clients.*', (cast((surname == matching_surname).as int) * 10 + cast((given_names == matching_given_names).as int) + cast((date_of_birth == matching_date_of_birth).as int).as(ranking)]}. where{(surname =~ matching_surname) | (given_names =~ matching_given_names) | (date_of_birth == matching_date_of_birth)}. order{`ranking`.desc} My

Can you add clauses in a where block conditionally when using Squeel?

独自空忆成欢 提交于 2019-12-13 13:14:56
问题 To start, I'm using Rails v3.2.9 with Squeel 1.0.13 and here's what I'm trying to do: I want to search for a client using any of three pieces of identifying information - name, date of birth (dob), and social insurance number (sin). The result set must include any record that has any of the identifier - an OR of the conditions. I have done this in Squeel before and it would look something like: scope :by_any, ->(sin, name, dob){ where{(client.sin == "#{sin}") | (client.name =~ "%#{name}%") |

Translate complex SQL query using SET to Arel or Squeel

[亡魂溺海] 提交于 2019-12-13 04:55:24
问题 How can I translate the given query using AREL or Squeel: SET @start = '2013-05-14'; SET @end = '2013-11-01'; SET @days = DATEDIFF(@end, @start); SET @UnusedDays = 0; SELECT @UnusedDays := DATEDIFF(end_at,@end) FROM PERIODS WHERE (@end > start_at AND @end <= end_at); SELECT @UnusedDays := @UnusedDays + DATEDIFF(@start, start_at) FROM PERIODS WHERE (@start >= start_at AND @start < end_at); SELECT @days + @UnusedDays - SUM(DATEDIFF(end_at, start_at)) AS Shortfall FROM PERIODS WHERE (@start >=

How do I handle the where conditions dynamically in squeel?

偶尔善良 提交于 2019-12-12 18:24:32
问题 From this input: {'hearing' => 1} I need to generate this query Score.joins(:target_disability).where{ (target_disabilities.name == 'hearing') & (round(total_score) >= 1) } From this input is {'hearing' => 1, 'mobility' => 2}, I need to generate this: Score.joins(:target_disability).where{ (target_disabilities.name == 'hearing') & (round(total_score) >= 1) | (target_disabilities.name == 'mobility') & (round(total_score) >= 2) } And so on... How can this be generalized? Because my input

Multiple joins to the same model using multiple belongs_to associations

六眼飞鱼酱① 提交于 2019-12-12 01:07:16
问题 I have a class Agreement that has two belongs_to associations to a Member class - referenced as primary and secondary: class Agreement < ActiveRecord::Base belongs_to :primary, class_name: 'Member' belongs_to :secondary, class_name: 'Member' ... def self.by_member(member_attribute_hash) # returns any agreements that has a primary OR secondary member that matches any of the values # in the member_attribute_hash ... end end The Member class has no knowledge of the association with the Agreement