activerecord

Fetch posts from non blocking users

守給你的承諾、 提交于 2021-01-27 23:42:32
问题 I'm trying to fetch some posts from users that isn't blocking "me". Se models below: User id username .... Post id user_id content ... Blockings blocker_id blocked_id I need to fetch posts from all users that isn't blocking me. I fetch all posts with: @posts Post.all But how do I joins this together. Pseudo SELECT * FROM posts WHERE "posts.user_id isn't blocking me" I have a helper called current_user that returns the current logged in user "me". 回答1: A way to do it with SQL would be: select

Select 5 of each distinct value

蓝咒 提交于 2021-01-27 23:11:29
问题 I have the following table in PostgreSQL: | a | b | c | =================== | 'w' | 2 | 3 | | 'w' | 7 | 2 | | 'w' | 8 | 1 | | 'w' | 3 | 6 | | 'w' | 0 | 8 | | 'w' | 2 | 9 | | 'w' | 2 | 9 | | 'z' | 4 | 9 | | 'z' | 0 | 9 | | 'z' | 0 | 8 | | 'z' | 3 | 6 | | 'z' | 2 | 7 | | 'z' | 3 | 1 | | 'z' | 3 | 2 | | 'z' | 3 | 3 | I want to select all records, but limit them to 5 records for each distinct value in column a . So the result would look like: | a | b | c | =================== | 'w' | 2 | 3 | | 'w

Rails has_one build association deletes existing record even if new record is not valid

醉酒当歌 提交于 2021-01-27 22:27:02
问题 I have seen that in Rails (5.2.1 at least) if you have a model with a has_one association to another model, the following happens: class Car < ApplicationRecord has_one :steering_wheel end class SteeringWheel < ApplicationRecord belongs_to :car validate_presence_of :name end And I have an existing car object with a steering wheel. Then I try to build a new steering wheel like so: car.build_steering_wheel The new steering wheel I am trying to build is not valid because I did not set the name

How should I use Rails to index and query a join table?

半世苍凉 提交于 2021-01-27 04:16:56
问题 I have a ruby on Rails 4 app, using devise and with a User model and a Deal model. I am creating a user_deals table for has_many/has_many relationship between User and Deal. Here is the migration class CreateUserDeals < ActiveRecord::Migration def change create_table :user_deals do |t| t.belongs_to :user t.belongs_to :deal t.integer :nb_views t.timestamps end end end When a user load a Deal (for example Deal id= 4), I use a method called show controllers/deal.rb #for the view of the Deal page

ActiveRecord: query not using correct type condition for STI subclass

我的梦境 提交于 2021-01-27 00:33:07
问题 I have a set of STI subclasses inheriting from a User base class. I am finding that under certain conditions inside a subclass' definition, queries on the subclasses do not correctly use the type condition. class User < ActiveRecord::Base # ... end class Admin < User Rails.logger.info "#{name}: #{all.to_sql}" # ... end When loading the Rails console in development, it does what I would expect: Admin: SELECT `users`.* FROM `users` WHERE `users`.`type` IN ('Admin') But when hitting the app

Find user who has no post in Rails

匆匆过客 提交于 2021-01-23 08:37:41
问题 This the the database relation: class User < ActiveRecord::Base has_many :posts end class Post < ActiveRecord::Base belongs_to :user end I come across a functionality where I want to query all the users who don't have any posts yet. I know that we can do this with something like this: users = User.all users.each do |user| unless user.posts.any? # do something when user don't have any post. end end However, I wonder if there is any way to optimize this by using one query only. Thanks! 回答1:

Find user who has no post in Rails

人走茶凉 提交于 2021-01-23 08:34:48
问题 This the the database relation: class User < ActiveRecord::Base has_many :posts end class Post < ActiveRecord::Base belongs_to :user end I come across a functionality where I want to query all the users who don't have any posts yet. I know that we can do this with something like this: users = User.all users.each do |user| unless user.posts.any? # do something when user don't have any post. end end However, I wonder if there is any way to optimize this by using one query only. Thanks! 回答1:

Find user who has no post in Rails

懵懂的女人 提交于 2021-01-23 08:33:07
问题 This the the database relation: class User < ActiveRecord::Base has_many :posts end class Post < ActiveRecord::Base belongs_to :user end I come across a functionality where I want to query all the users who don't have any posts yet. I know that we can do this with something like this: users = User.all users.each do |user| unless user.posts.any? # do something when user don't have any post. end end However, I wonder if there is any way to optimize this by using one query only. Thanks! 回答1:

SpringBoot结合MyBatis Plus 自动生成代码

偶尔善良 提交于 2021-01-17 08:39:04
SpringBoot结合MyBatis Plus 自动生成代码 本来这一章要介绍Redis+AOP优化权限,可是发现还是需要先介绍一些MyBatis Plus自动生成代码 MyBatis Plus简介 MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。 MyBatis Plus特性 无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑 损耗小:启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作 强大的 CRUD 操作:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求 支持 Lambda 形式调用:通过 Lambda 表达式,方便的编写各类查询条件,无需再担心字段写错 支持主键自动生成:支持多达 4 种主键策略(内含分布式唯一 ID 生成器 - Sequence),可自由配置,完美解决主键问题 支持 ActiveRecord 模式:支持 ActiveRecord 形式调用,实体类只需继承 Model 类即可进行强大的 CRUD 操作 支持自定义全局通用操作:支持全局通用方法注入( Write once,

How to limit has_one association to a single record

馋奶兔 提交于 2021-01-04 18:45:09
问题 I am having trouble with retaining only a single record in a has_one relationship Example Categories and question A question belongs_to a category, and a category has_one question In reality as seen in a system i am building even though the has_one relationship exists, there comes a case where i have multiple questions that belong to a category. Shouldn't the has_one relationship limit them to just one record? If not, then how can i make sure that i always keep one record? EDIT Please note