squeel

Squeel evaluating a string as a keypath for joins

两盒软妹~` 提交于 2019-12-11 01:31:06
问题 Here's what I'd really like to do: Outage.joins{eval "unit.plant"} I'm going to have a string that represents a squeel keypath . I'd like to send this string into a join statement. But here's what I get as an error: #<Class:0x639e328>: unknown class: Squeel::Nodes::Function I've tried: Outage.joins{"unit.plant"} But that does not work... what am I missing? I don't see any documentation on this on the github page: https://github.com/ernie/squeel/ Thank you for any help! Update: I've found a

Optimize difficult query (possibly with squeel)

大城市里の小女人 提交于 2019-12-10 16:16:31
问题 There is such code(using PublicActivity gem & Squeel) def index @activities = Activity.limit(20).order { created_at.desc } @one = @activities.where{trackable_type == 'Post'}.includes(trackable: [:author, :project]) @two = @activities.where{trackable_type == 'Project'}.includes trackable: [:owner] @activities = @one + @two end But it creates 8 SQL requests: SELECT "activities".* FROM "activities" WHERE "activities"."trackable_type" = 'Post' ORDER BY "activities"."created_at" DESC LIMIT 20

Performing multiple joins on the same association with Squeel

我的梦境 提交于 2019-12-08 02:36:20
问题 In my application, I have to models: Workflow and Step; steps belongs_to workflow and a workflow has_many steps. Steps have an index and a boolean status ('completed'). I want to retrieve workflows whose step 1 is completed and step 2 is not, i.e. something like this in SQL: SELECT * FROM workflows w INNER JOIN steps s1 ON s1.workflow_id = w.id INNER JOIN steps s2 ON s2.workflow_id = w.id WHERE s1.index = 1 AND s1.completed = 1 AND s2.index = 2 AND s2.completed = 0 I've tried to express this

Nested query in squeel

折月煮酒 提交于 2019-12-07 15:24:49
问题 Short version: How do I write this query in squeel? SELECT OneTable.*, my_count FROM OneTable JOIN ( SELECT DISTINCT one_id, count(*) AS my_count FROM AnotherTable GROUP BY one_id ) counts ON OneTable.id=counts.one_id Long version: rocket_tag is a gem that adds simple tagging to models. It adds a method tagged_with . Supposing my model is User , with an id and name, I could invoke User.tagged_with ['admin','sales'] . Internally it uses this squeel code: select{count(~id).as(tags_count)}

How to dynamically generate association names?

扶醉桌前 提交于 2019-12-07 00:23:25
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 < ActiveRecord::Base def my_squeel_query commenters. .where{ model_as_like_article_comment_associations.article

Performing multiple joins on the same association with Squeel

爱⌒轻易说出口 提交于 2019-12-06 13:15:34
In my application, I have to models: Workflow and Step; steps belongs_to workflow and a workflow has_many steps. Steps have an index and a boolean status ('completed'). I want to retrieve workflows whose step 1 is completed and step 2 is not, i.e. something like this in SQL: SELECT * FROM workflows w INNER JOIN steps s1 ON s1.workflow_id = w.id INNER JOIN steps s2 ON s2.workflow_id = w.id WHERE s1.index = 1 AND s1.completed = 1 AND s2.index = 2 AND s2.completed = 0 I've tried to express this query with Squeel, but it seems it does not allow multiple joins on the same association: I cannot find

Nested query in squeel

佐手、 提交于 2019-12-05 18:38:18
Short version: How do I write this query in squeel? SELECT OneTable.*, my_count FROM OneTable JOIN ( SELECT DISTINCT one_id, count(*) AS my_count FROM AnotherTable GROUP BY one_id ) counts ON OneTable.id=counts.one_id Long version: rocket_tag is a gem that adds simple tagging to models. It adds a method tagged_with . Supposing my model is User , with an id and name, I could invoke User.tagged_with ['admin','sales'] . Internally it uses this squeel code: select{count(~id).as(tags_count)} .select("#{self.table_name}.*"). joins{tags}. where{tags.name.in(my{tags_list})}. group{~id} Which generates

Ordering nested has_many :through by count of associations

这一生的挚爱 提交于 2019-12-04 06:21:19
问题 I'm trying to order Tags by order of the descending frequency of their association with Users of a specific Group. (ActiveRecord & Rails 3.2 - I also have Squeel installed if that helps!) Users have Tags, Groups have Users. Groups thus have Tags through Users. class Group < ActiveRecord::Base has_many :users_groups # join table has_many :users, through: :users_groups has_many :tags, through: :users class User < ActiveRecord::Base has_many :users_groups # join table has_many :groups, through:

Ranking results with complex conditions using Rails and Squeel

拥有回忆 提交于 2019-12-02 09:43:33
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 problem is that date_of_birth could be nil. This causes the cast((...).as int) call to return three

Ordering nested has_many :through by count of associations

有些话、适合烂在心里 提交于 2019-12-02 08:31:34
I'm trying to order Tags by order of the descending frequency of their association with Users of a specific Group. (ActiveRecord & Rails 3.2 - I also have Squeel installed if that helps!) Users have Tags, Groups have Users. Groups thus have Tags through Users. class Group < ActiveRecord::Base has_many :users_groups # join table has_many :users, through: :users_groups has_many :tags, through: :users class User < ActiveRecord::Base has_many :users_groups # join table has_many :groups, through: :users_groups has_many :taggings # join table has_many :tags, through: taggings class Tag <