# Rails 명령어로 사용자를 대량으로 TL2로 승급하는 방법은?

**URL:** https://meta.discourse.org/t/rails-command-to-mass-promote-users-to-tl2/149238
**Category:** Development
**Tags:** rails-console
**Created:** [4월 25, 2020, 8:07오전 UTC](https://meta.discourse.org/t/rails-command-to-mass-promote-users-to-tl2/149238 "2020-04-25T08:07:07Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![Queth](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/queth/32/154753_2.png) [@Queth](https://meta.discourse.org/u/Queth)
#### Post date: [4월 25, 2020, 8:07오전 UTC](https://meta.discourse.org/t/rails-command-to-mass-promote-users-to-tl2/149238/1 "2020-04-25T08:07:08Z")

</div>

안녕하세요, 이전 주제인 [이전 주제](https://meta.discourse.org/t/mass-promotion-to-tl2-via-manual-command/147909)에서 이 문제에 대해 질문을 했으나, 제가 처음에 해결됨으로 표시한 후로 질문이 무시된 것 같아서, 실제로는 해결되지 않았기 때문에 새로운 주제를 개설합니다.

상황은 다음과 같습니다: 저는 Discourse로 이전했으며, 기존 포럼의 모든 멤버가 TL1 등급을 가지고 있습니다. 멤버들은 이것이 공정하지 않다고 느끼고 있으므로, 요구 조건을 변경하여 모두를 TL2로 승급시키고 싶었습니다. 이전 작업을 위해 계정을 비활성화해야 했기 때문에 상당수의 멤버가 비활성 상태입니다.

TL2 요구 조건을 게시 횟수 기준으로만 변경하고, 방문 일수는 0으로 설정했습니다.

10일 후에도 소수의 사용자만 승급되었으므로, TL2 확인이 이루어지려면 로그인하거나 답글을 게시해야 하는 것 같습니다. (아직 정확히 어느 조건인지 파악하지 못했습니다)

저는 기존 포럼의 모든 멤버에게 TL2 상태를 부여하고 싶습니다. 새로운 TL2 기준을 충족하는 사용자를 찾아서 승급하는 명령어를 어딘가에서 실행할 수 있는 방법이 있을까요?

도움과 조언을 미리 진심으로 감사드립니다! 🙂

---

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [4월 25, 2020, 9:25오전 UTC](https://meta.discourse.org/t/rails-command-to-mass-promote-users-to-tl2/149238/2 "2020-04-25T09:25:52Z")

</div>

아마도:

```ruby
users = User.where('admin = false')
users.update_all(trust_level: 2)

```

---

<div class="post-metadata">

### Author: ![neounix](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/neounix/32/215617_2.png) [@neounix](https://meta.discourse.org/u/neounix)
#### Post date: [4월 25, 2020, 9:34오전 UTC](https://meta.discourse.org/t/rails-command-to-mass-promote-users-to-tl2/149238/3 "2020-04-25T09:34:42Z")

</div>

> [@Canapin](#):
>
> users = User.where(‘admin = false’) users.update\_all(trust\_level: 2)

이 쿼리는 모든 모더레이터의 신뢰 수준도 2로 변경합니다.

@Queth가 실제로 그렇게 하길 원하는지 확신할 수 없습니다.

---

<div class="post-metadata">

### Author: ![dax](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dax/32/244677_2.png) [@dax](https://meta.discourse.org/u/dax)
#### Post date: [4월 25, 2020, 9:43오전 UTC](https://meta.discourse.org/t/rails-command-to-mass-promote-users-to-tl2/149238/4 "2020-04-25T09:43:43Z")

</div>

하위 신뢰 수준에서 상위 신뢰 수준으로 모든 사용자를 일괄 이동하는 방법(그리고 그 반대)에 대한 안내문이 있습니다.

> [@Modify trust level for all users](https://meta.discourse.org/t/how-to-modify-trust-level-for-all-users/103628):
>
> Here’s an example of a [bulk operation](https://meta.discourse.org/t/administrative-bulk-operations/118349) to set the trust level to 2 for all users currently at trust level 0 or 1: This is slow, but properly triggers all side effects (group membership updates, badge grants, and logging). It may be preferred especially when you have fewer than 1000 users. Note: this does not skip users with manually locked trust levels — add .where(manual\_locked\_trust\_level: nil) to the query if you want to skip them. User.where(trust\_level: [0, 1]).find\_each do |user| user.…

---

<div class="post-metadata">

### Author: ![Queth](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/queth/32/154753_2.png) [@Queth](https://meta.discourse.org/u/Queth)
#### Post date: [4월 25, 2020, 10:20오전 UTC](https://meta.discourse.org/t/rails-command-to-mass-promote-users-to-tl2/149238/5 "2020-04-25T10:20:12Z")

</div>

빠른 답변 정말 감사합니다!

@Canapin, @neounix

일시적으로 모드의 등급을 TL2로 변경하는 데에는 문제가 없습니다. 나중에 수동으로 그들을 TL1로 되돌릴 수 있을 것 같아요. 이렇게 하면 모드들의 권한에 영향을 주지 않을 거라고 생각하는데, 맞나요?

@dax

감사합니다! 총 2만 명의 사용자를 말씀하시는 건데, 마이그레이션 후 1만 명이 TL1이고 나머지 1만 명이 0입니다. 저는 TL1인 사용자들만 승급시키고 싶은데, 이 쿼리로 가능할까요?

```
User.exec_sql("UPDATE users SET trust_level = 2 WHERE trust_level IN ( 1)")

```

또 다른 점은, 그 사이에 “실제” 신규 멤버들이 가입했는데, 이들이 노력해서 얻지 못한 TL2로 승급되는 것은 원하지 않습니다. 😉  
가입일 기준을 포함할 수 있는 방법이 있을까요? 예를 들어 2020년 3월 17일 이전에 가입한 모든 TL1 사용자처럼요?

---

<div class="post-metadata">

### Author: ![neounix](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/neounix/32/215617_2.png) [@neounix](https://meta.discourse.org/u/neounix)
#### Post date: [4월 25, 2020, 10:23오전 UTC](https://meta.discourse.org/t/rails-command-to-mass-promote-users-to-tl2/149238/6 "2020-04-25T10:23:09Z")

</div>

> [@Queth](#):
>
> 등록 날짜 기준을 포함하는 방법이 있을까요? 예를 들어 2020년 3월 17일 이전에 등록한 모든 TL1 사용자를 조회하는 것 같은 경우요.

네, @Queth. users 테이블 구조의 사본을 게시해 드릴까요? 그러면 거기에 있는 모든 항목을 확인하실 수 있습니다.

업데이트: @Queth, 여기 있습니다. 테이블 설명을 보시면 선택할 수 있는 옵션이 많다는 것을 아실 수 있습니다 🙂

> **users table**
>
> ```plaintext
> discourse=# \d users
> 
> Table "public.users"
> Column | Type | Collation | Nullable | Default              
> ---------------------------+-----------------------------+-----------+----------+-----------------------------------
> id | integer | | not null | nextval('users_id_seq'::regclass)
> username | character varying(60) | | not null | 
> created_at | timestamp without time zone | | not null | 
> updated_at | timestamp without time zone | | not null | 
> name | character varying | | | 
> seen_notification_id | integer | | not null | 0
> last_posted_at | timestamp without time zone | | | 
> password_hash | character varying(64) | | | 
> salt | character varying(32) | | | 
> active | boolean | | not null | false
> username_lower | character varying(60) | | not null | 
> last_seen_at | timestamp without time zone | | | 
> admin | boolean | | not null | false
> last_emailed_at | timestamp without time zone | | | 
> trust_level | integer | | not null | 
> approved | boolean | | not null | false
> approved_by_id | integer | | | 
> approved_at | timestamp without time zone | | | 
> previous_visit_at | timestamp without time zone | | | 
> suspended_at | timestamp without time zone | | | 
> suspended_till | timestamp without time zone | | | 
> date_of_birth | date | | | 
> views | integer | | not null | 0
> flag_level | integer | | not null | 0
> ip_address | inet | | | 
> moderator | boolean | | | false
> title | character varying | | | 
> uploaded_avatar_id | integer | | | 
> locale | character varying(10) | | | 
> primary_group_id | integer | | | 
> registration_ip_address | inet | | | 
> staged | boolean | | not null | false
> first_seen_at | timestamp without time zone | | | 
> silenced_till | timestamp without time zone | | | 
> group_locked_trust_level | integer | | | 
> manual_locked_trust_level | integer | | | 
> secure_identifier | character varying | | | 
> Indexes:
> "users_pkey" PRIMARY KEY, btree (id)
> "index_users_on_secure_identifier" UNIQUE, btree (secure_identifier)
> "index_users_on_username" UNIQUE, btree (username)
> "index_users_on_username_lower" UNIQUE, btree (username_lower)
> "idx_users_admin" btree (id) WHERE admin
> "idx_users_moderator" btree (id) WHERE moderator
> "index_users_on_last_posted_at" btree (last_posted_at)
> "index_users_on_last_seen_at" btree (last_seen_at)
> "index_users_on_uploaded_avatar_id" btree (uploaded_avatar_id)
> Referenced by:
> TABLE "user_security_keys" CONSTRAINT "fk_rails_90999b0454" FOREIGN KEY (user_id) REFERENCES users(id)
> TABLE "poll_votes" CONSTRAINT "fk_rails_b64de9b025" FOREIGN KEY (user_id) REFERENCES users(id)
> TABLE "bookmarks" CONSTRAINT "fk_rails_c1ff6fa4ac" FOREIGN KEY (user_id) REFERENCES users(id)
> 
> ```

항상 먼저 SELECT를 실행하여 UPDATE를 수행하기 전에 쿼리가 원하는 결과를 반환하는지 확인하는 것이 좋습니다 :). 그리고 대량 DB 변경 작업을 시작하기 전에는 항상 전체 백업을 만들어 두세요. 실수(Fat finger mistakes)는 최고의 전문가에게도 일어날 수 있습니다. 전문가와 초보자의 주요 차이점 중 하나는 전문가가 항상 먼저 백업을 만든다는 점입니다 🙂

---

<div class="post-metadata">

### Author: ![Queth](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/queth/32/154753_2.png) [@Queth](https://meta.discourse.org/u/Queth)
#### Post date: [4월 25, 2020, 11:23오전 UTC](https://meta.discourse.org/t/rails-command-to-mass-promote-users-to-tl2/149238/7 "2020-04-25T11:23:19Z")

</div>

@neounix 님 감사합니다!

올바른 쿼리에 대해 도움을 주실 수 있을까요? 어떻게 해야 하는지 모르겠거든요… 아마 어딘가에

```
Where user created at < [2020년 3월 17일, 하지만 올바르게 포맷된 형태] 를 포함해야 할 것 같습니다.

```

---

<div class="post-metadata">

### Author: ![neounix](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/neounix/32/215617_2.png) [@neounix](https://meta.discourse.org/u/neounix)
#### Post date: [4월 25, 2020, 12:25오후 UTC](https://meta.discourse.org/t/rails-command-to-mass-promote-users-to-tl2/149238/8 "2020-04-25T12:25:33Z")

</div>

> [@Queth](#):
>
> 사용자 생성일이 2020년 3월 17일보다 이전인 경우 (다만, 이후 올바르게 포맷된 것)

저도 도와드리고 싶습니다. 하지만 일부 작업은 여러분이 스스로 조금 더 조사하는 것이 더 쉽고(또는 더 좋습니다). 퍼즐 조각은 이미 모두 가지고 있으므로, 제 조언은 시간을 좀 내서 SQL 쿼리를 작성하는 법을 배우라는 것입니다. 솔직히 말하자면, 이는 온라인의 수많은 튜토리얼에서 쉽게 찾을 수 있는 PostgreSQL SQL 입문 수준의 내용입니다.

이런 “튜토리얼 101” 같은 내용을 반복해서 알려드리는 것은 제 역할이 아닙니다. 다른 사람들도 스스로 구글 검색을 통해 쉽게 찾을 수 있는 정보이니까요. 그냥 제 성격이 그렇습니다. 이해해 주셔서 감사드리며, 제 솔직한 의견에 대해 양해 부탁드립니다.

참고: SQL은 1974년, 즉 46년 전부터 존재해 왔습니다. DB 기반 애플리케이션을 관리한다면 기본적인 SQL 지식을 갖추는 것이 좋은 일입니다.

---

<div class="post-metadata">

### Author: ![Queth](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/queth/32/154753_2.png) [@Queth](https://meta.discourse.org/u/Queth)
#### Post date: [4월 25, 2020, 12:48오후 UTC](https://meta.discourse.org/t/rails-command-to-mass-promote-users-to-tl2/149238/9 "2020-04-25T12:48:22Z")

</div>

감사합니다 🙂  
기본적인 지식은 좀 있지만, 쿼리를 처음부터 직접 작성하는 것보다는 신뢰할 수 있는 출처에서 복사해서 붙여넣는 편을 더 선호해요.

그래도 이렇게 빌딩 블록을 알려주셔서 정말 감사합니다!

---

<div class="post-metadata">

### Author: ![neounix](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/neounix/32/215617_2.png) [@neounix](https://meta.discourse.org/u/neounix)
#### Post date: [4월 25, 2020, 12:50오후 UTC](https://meta.discourse.org/t/rails-command-to-mass-promote-users-to-tl2/149238/10 "2020-04-25T12:50:07Z")

</div>

> [@Queth](#):
>
> 감사합니다 🙂  
> 기본적인 지식은 조금 있지만, 쿼리를 처음부터 직접 작성하기보다는 신뢰할 수 있는 출처에서 복사해서 붙여넣는 편이 더 낫습니다.
> 
> 그래도 기본 요소들을 알려주셔서 정말 감사합니다!

항상 먼저 SELECT를 실행하여 쿼리가 원하는 결과를 내는지 확인한 뒤에 UPDATE를 하는 것이 좋습니다 🙂 그리고 대량의 DB 변경 작업을 시작하기 전에 반드시 전체 백업을 만들어 두세요.

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [6월 21, 2024, 8:17오전 UTC](https://meta.discourse.org/t/rails-command-to-mass-promote-users-to-tl2/149238/13 "2024-06-21T08:17:06Z")

</div>



---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [6월 21, 2024, 8:27오전 UTC](https://meta.discourse.org/t/rails-command-to-mass-promote-users-to-tl2/149238/14 "2024-06-21T08:27:33Z")

</div>


