activerecord

How to exclude rows when using a LEFT JOIN (MySQL)

心已入冬 提交于 2021-02-10 08:06:46
问题 I have users with many posts . I want to build an SQL query that would do the following in 1 query (no subquery), and hopefully no unions if possible. I know I can do this with union but I want to learn if this can be done using only joins. I want to get a list of distinct active users who: have no posts have no approved posts Here's what I have so far: SELECT DISTINCT u.* FROM users u LEFT JOIN posts p ON p.user_id = u.id LEFT JOIN posts p2 ON p2.user_id = u.id WHERE u.status = 'active' AND

How to exclude rows when using a LEFT JOIN (MySQL)

╄→尐↘猪︶ㄣ 提交于 2021-02-10 08:06:10
问题 I have users with many posts . I want to build an SQL query that would do the following in 1 query (no subquery), and hopefully no unions if possible. I know I can do this with union but I want to learn if this can be done using only joins. I want to get a list of distinct active users who: have no posts have no approved posts Here's what I have so far: SELECT DISTINCT u.* FROM users u LEFT JOIN posts p ON p.user_id = u.id LEFT JOIN posts p2 ON p2.user_id = u.id WHERE u.status = 'active' AND

How to exclude rows when using a LEFT JOIN (MySQL)

霸气de小男生 提交于 2021-02-10 08:04:02
问题 I have users with many posts . I want to build an SQL query that would do the following in 1 query (no subquery), and hopefully no unions if possible. I know I can do this with union but I want to learn if this can be done using only joins. I want to get a list of distinct active users who: have no posts have no approved posts Here's what I have so far: SELECT DISTINCT u.* FROM users u LEFT JOIN posts p ON p.user_id = u.id LEFT JOIN posts p2 ON p2.user_id = u.id WHERE u.status = 'active' AND

Rails querying an associated models scope

对着背影说爱祢 提交于 2021-02-10 06:39:06
问题 I am struggling to find a suitable solution for what should be a simple task. Essentially I have a category model which has many posts . The post belongs to the category . I am displaying categories as part of a search form as well as a number of other places and do not want to display categories that have no associated posts. That seems kind of pointless. I managed to solve this problem by adding the following method to my category model. # Check if Category has associated results def self

How to test conditional ActiveRecord after_update callback with rspec?

寵の児 提交于 2021-02-10 05:52:10
问题 I have a conditional callback that triggers a job. If the manager changes, it should call the method class Employee < ActiveRecord::Base after_update :employee_manager_on_change, if: :employee_id_changed? def employee_manager_on_change EmployeeManagerChangedJob.perform_later(id) end end I'm having trouble to test this. I needed something like context 'when changing manager' do subject { user.manager = new_manager } it 'calls employee_manager_on_change' do expect { suject.run_callbacks :update

How to make Low-Level caching collaborate with Association caching in Rails?

元气小坏坏 提交于 2021-02-09 09:25:12
问题 I am currently working on a project with Rails 5. I want to boost the performance, so I decided to use Low-Level caching like the following: class User < ApplicationRecord has_one :profile def cached_profile Rails.cache.fetch(['Users', id, 'profile', updated_at.to_i]) do profile end end end class Profile < ApplicationRecord belongs_to :user, touch: true end It works fine respectively. But now I want to make the two caches to collaborate. What I want is that the associated object doesn't need

How to make Low-Level caching collaborate with Association caching in Rails?

◇◆丶佛笑我妖孽 提交于 2021-02-09 09:21:22
问题 I am currently working on a project with Rails 5. I want to boost the performance, so I decided to use Low-Level caching like the following: class User < ApplicationRecord has_one :profile def cached_profile Rails.cache.fetch(['Users', id, 'profile', updated_at.to_i]) do profile end end end class Profile < ApplicationRecord belongs_to :user, touch: true end It works fine respectively. But now I want to make the two caches to collaborate. What I want is that the associated object doesn't need

How to change ActiveRecord's table name during runtime

喜你入骨 提交于 2021-02-07 05:51:53
问题 I am changing the table_name_prefix during running of a rails application (might sound weird, but really that's what I want). When the table_name_prefix changes for the ActiveRecord I reset the table names ( table_name and quoted_table_name ) by calling reset_table_name , and they change.. however I have another issue. If the table name changes, after calling such a thing like count or a find the ActiveRecord object still operates with the table, which was used before. How can reach to reset

Sortable UUIDs and overriding ActiveRecord::Base

若如初见. 提交于 2021-02-06 20:00:45
问题 I'm wanting to use UUIDs in an app I'm building and am running into a bit of a problem. Due to UUIDs (v4) not being sortable because they're randomly generated, I'm trying to override ActiveRecord::Base#first, but Rails isn't too pleased with that. It yells at me saying ArgumentError: You tried to define a scope named "first" on the model "Item", but Active Record already defined a class method with the same name. Do I have to use a different method if I want to sort and have it sort

Sortable UUIDs and overriding ActiveRecord::Base

非 Y 不嫁゛ 提交于 2021-02-06 20:00:26
问题 I'm wanting to use UUIDs in an app I'm building and am running into a bit of a problem. Due to UUIDs (v4) not being sortable because they're randomly generated, I'm trying to override ActiveRecord::Base#first, but Rails isn't too pleased with that. It yells at me saying ArgumentError: You tried to define a scope named "first" on the model "Item", but Active Record already defined a class method with the same name. Do I have to use a different method if I want to sort and have it sort