笔记---ruby+pgsql+mysql

这篇笔记涵盖了Ruby编程中的实用技巧,包括数组操作如partition和flatten的组合使用,模型的类方法和实例方法的区别,字符串处理如lstrip、rstrip和gsub,以及JSON和JSONB的区别。还涉及到PostgreSQL数据库的查询,如查询所有表并设置ID自增,以及数据库迁移的操作。此外,笔记讨论了Rails框架中的预加载、联合查询和JOIN,以及多态关联的实现。最后提到了Ruby字符串的修改方法和数据库账户密码的修改命令。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

笔记

随便记录的一些东西…

1、partition和flatten组合使用

partition()将数组按照条件分成组,符合条件的组在前;flatten()将多维的数组展开成一维的数组

partition{|x| x.* == true}.flatten()

2、model的类方法和实例方法

self.** 是类方法,** 是实例方法。类方法前面加self.;

3、ruby去空格

lstrip : 去掉首空格
rstrip : 去掉尾空格
gsub : 去掉全部空格,不过要用到pattern匹配

a = "     hello    world    "
 
puts a.lstrip   =>  "hello    world    "
 
puts a.rstrip   =>  "     hello    world"
 
puts a.lstrip.rstrip   =>   "hello    world"
 
puts a.gsub(/\s+/,'')  =>  "helloworld"
 
puts a.gsub(' ','')   =>  "helloworld"
 
puts a.gsub(//,'')   =>  "helloworld"

4、json和jsonb的区别

https://blog.csdn.net/qq_17021569/article/details/106110258

5、查询pg数据库的所有表并设置id从现有最大数据自增长

tables = User.connection.execute("select tablename from pg_tables where tablename not like 'pg_%' and tablename not like '%sql%'
    and tablename not like 'schema_%' and tablename not like 'authorization_%' and tablename not like 'ar_%'
    and tablename not like 'bad_%' and tablename not like 'child_%'  and tablename not like 'confluence_%'
    and tablename not like 'contents_%' and tablename not like 'package%' and tablename not like 'survey%'
    ").to_a
tables.map{|x| x['tablename']}.each do |table|
  User.connection.execute("SELECT setval('#{table}_id_seq', (SELECT MAX(id) FROM #{table})+1,false)")
end

6、迁移

rake db:migrate
rake db:migrate:down VERSION=20200309****43
rake db:migrate:up VERSION=20200309****43

7、查看迁移的版本记录

select * from leads.schema_migrations

8、model层的scope中不要加 .last和 .first

9、hash的部分操作

https://www.jianshu.com/p/a6486bedf14b

stringify_keys可以將hash中的鍵值改為字串
symbolize_keys則是會把hash中的鍵值都呼叫to_sym方法將之改為symbol

10、preload、 eager_load、(includes + references) 和 joins

https://www.jianshu.com/p/9ee5ffcad526?from=timeline&isappinstalled=0

11、两个表关联查询并涉及关联表的字段筛选和排序

#  row_number() over(partition by t1.id order by t2.created_at desc) as rn
select * from (select id,user_id,row_number() over(partition by user_id order by created_at asc)rn from ap..._histories where created_at > '2020-9-19 00:00') tt where tt.rn = 1
"select * from tables order by created_at desc nulls last"

12、rails脚手架

rails g scaffold Cfg key:string value:string

13、ruby string 的修改

chomp:去掉字符串末尾的\n或\r
chop:去掉字符串末尾的最后一个字符,不管是\n\r还是普通字符
gsub('/', '-')

14、Rails多态关联在同一模型上具有多个关联

数据库是Mysql数据库
一个模型上和另外一个模型进行多个不同的关联

class User< ApplicationRecord
	has_paper_trail
	has_many :attachments, -> { where(a_type: "attachments")}, as: :belongable, dependent: :destroy
  	has_many :other_attachment, -> { where(a_type: "other_attachment")}, class_name: "Attachment", as: :belongable, dependent: :destroy
end

下面这种方式尝试之后报错,可能使用方式和版本不匹配

has_one :photo, :as => 'attachable',
        :conditions => {:photo_type => 'primary_photo'}, :dependent => :destroy

has_one :secondary_photo, :class_name => 'Photo', :as => 'attachable',
        :conditions => {:photo_type => 'secondary_photo'}, :dependent => :destroy

参考链接:https://www.codenong.com/2494452/

15、underscore、constantize

# 驼峰命名法的字符串转成下划线组织的字符串
'UserLog'.underscore  # => 'user_log'

# 字符串转成类名
'UserLog'.constantize  # => UserLog

16、mysql修改账户密码的命令

如果忘记密码,可能需要以不安全的方式启动 MySQL 服务。(以Ubuntu22.04上的mysql8.0.35为例)

# 停止 MySQL 服务
sudo service mysql stop 

# 以不安全的方式启动 MySQL 服务
sudo mysqld_safe --skip-networking &

# 打开另一个终端窗口并连接到 MySQL
mysql -u root

执行以下 SQL 命令来设置 root 用户的密码

ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' BY '123456';
FLUSH PRIVILEGES;

修改完退出后

# 停止之前以不安全方式启动的 MySQL 进程
sudo pkill mysqld_safe

# 重新启动 MySQL 服务:
sudo service mysql start

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值