# Rails console commands

**URL:** https://meta.discourse.org/t/rails-console-commands/300682
**Category:** Development
**Tags:** rails-console
**Created:** [March 23, 2024, 11:40pm UTC](https://meta.discourse.org/t/rails-console-commands/300682 "2024-03-23T23:40:01Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![Isambard](https://avatars.discourse-cdn.com/v4/letter/i/858c86/32.png) [@Isambard](https://meta.discourse.org/u/Isambard)
#### Post date: [March 23, 2024, 11:40pm UTC](https://meta.discourse.org/t/rails-console-commands/300682/1 "2024-03-23T23:40:01Z")

</div>

I found the rails console and learned that you can do blackmagic such as:

```plaintext
DB.exec(<<~SQL)
  UPDATE topics
  SET title = 'psql redo!'
  WHERE id = 886
SQL

```

How do I find out what commands/functions can be executed in the console? How do I learn more about this wizardry?

Note: if not already obvious, I know nothing about Ruby or Rails.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [March 24, 2024, 12:38am UTC](https://meta.discourse.org/t/rails-console-commands/300682/2 "2024-03-24T00:38:34Z")

</div>

You really need to learn about rails and not directly modify that database. What I did was find a rails book and read it. And at some point I also looked at a Ruby book. I’m old enough to remember when books were the only way to learn about computers. As I understand it, there are many sites on the internet that can give you a Rails tutorial. I think that there is a way that you can search the internet to find them.

Here is one that I found

> **[Ruby on Rails Guides](https://guides.rubyonrails.org/)**
>
> Ruby on Rails Guides

Here’s how to update stuff with rails, but reading one of those guides will quickly give you an idea of how it works.

```plaintext
t=Topic.find(123)
t.title='my new title'
t.save

# or
t.update(title: 'my new title') 

```

When you do stuff through Rails it does a bunch of checks if stuff. These things will usually keep you from making huge mistakes. You probably shouldn’t break those rules until you understand them.

For example, if you want users to create topics in a certain category via the API, then they need to have rights to create a topic in that category. If you really want to have a topic created by a user to exist in a category he doesn’t have rights to, then create it if another category and then have an admin move it (or do it via rails, which won’t enforce category permissions).

---

<div class="post-metadata">

### Author: ![thoka](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/thoka/32/115652_2.png) [@thoka](https://meta.discourse.org/u/thoka)
#### Post date: [March 24, 2024, 6:12am UTC](https://meta.discourse.org/t/rails-console-commands/300682/3 "2024-03-24T06:12:04Z")

</div>

Rails console speaks ruby.

As Discourse is open source, you can learn  
low level programming it by reading the source code in GitHub.

---

<div class="post-metadata">

### Author: ![Isambard](https://avatars.discourse-cdn.com/v4/letter/i/858c86/32.png) [@Isambard](https://meta.discourse.org/u/Isambard)
#### Post date: [March 24, 2024, 8:01am UTC](https://meta.discourse.org/t/rails-console-commands/300682/4 "2024-03-24T08:01:10Z")

</div>

t=Topic.find(123)  
t.title=‘my new title’  
t.save

# or

t.update(title: ‘my new title’)

Thanks. One thing I couldn’t figue out was how to assign a value with a value pulled from somewhere e.g.

`t.update(title: topic2.find(123).title)`

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [March 24, 2024, 9:14am UTC](https://meta.discourse.org/t/rails-console-commands/300682/5 "2024-03-24T09:14:21Z")

</div>

You could do something like:

```ruby
t = Topic.find(123)
t.update(title: Topic.find(124).title)

```

But that’s more trouble than it’s worth. You can do things in steps:

```ruby
t1 = Topic.find(123)
t2 = Topic.find(124)
t1.update(title: t2.title)

```

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [March 24, 2024, 9:22am UTC](https://meta.discourse.org/t/rails-console-commands/300682/6 "2024-03-24T09:22:59Z")

</div>

> [@pfaffman](#):
>
> What I did was find a rails book and read it.

Me too: [About this Book :: Why's (Poignant) Guide to Ruby](https://poignant.guide/book/chapter-1.html)

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [March 24, 2024, 10:28am UTC](https://meta.discourse.org/t/rails-console-commands/300682/7 "2024-03-24T10:28:08Z")

</div>

> [@simon](#):
>
> `t1 = Topic.find(123)`

Although this could easily cause an exception (which you are advised to rescue), so you might want to use `find_by` as an alternative which will return `nil` rather than raising an exception, depending on how you want to manage things.

---

<div class="post-metadata">

### Author: ![Isambard](https://avatars.discourse-cdn.com/v4/letter/i/858c86/32.png) [@Isambard](https://meta.discourse.org/u/Isambard)
#### Post date: [March 24, 2024, 7:30pm UTC](https://meta.discourse.org/t/rails-console-commands/300682/8 "2024-03-24T19:30:43Z")

</div>

Man. I always thought RoR was a relic from the noughties, I never imagined finding myself learning this! 🙂

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [March 24, 2024, 9:51pm UTC](https://meta.discourse.org/t/rails-console-commands/300682/9 "2024-03-24T21:51:15Z")

</div>

Sql is a relic from the 1960s. 😜

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [March 24, 2024, 10:00pm UTC](https://meta.discourse.org/t/rails-console-commands/300682/10 "2024-03-24T22:00:30Z")

</div>

“Niche” pays bills!

---

<div class="post-metadata">

### Author: ![Isambard](https://avatars.discourse-cdn.com/v4/letter/i/858c86/32.png) [@Isambard](https://meta.discourse.org/u/Isambard)
#### Post date: [March 24, 2024, 10:10pm UTC](https://meta.discourse.org/t/rails-console-commands/300682/11 "2024-03-24T22:10:31Z")

</div>

I’m actually using AI to write SQL queries. It can do it way faster than me!

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [March 25, 2024, 12:09pm UTC](https://meta.discourse.org/t/rails-console-commands/300682/12 "2024-03-25T12:09:56Z")

</div>

That’s how we’re the same. I needed to do some sql with a complicated inner join and another with a simple union that I got the ai to help with. I shaker if I had paid a bit more attention in my database class in 1986 I’d have known how to do it. 🤣

Why not get the ai to write rails commands for you?

---

<div class="post-metadata">

### Author: ![Isambard](https://avatars.discourse-cdn.com/v4/letter/i/858c86/32.png) [@Isambard](https://meta.discourse.org/u/Isambard)
#### Post date: [March 25, 2024, 1:15pm UTC](https://meta.discourse.org/t/rails-console-commands/300682/13 "2024-03-25T13:15:05Z")

</div>

For standard select, where, sort, limit queries, I write this myself, but anything more complicated, I now as the AI. I asked the AI:

> write a query that shows a category and then lists the top 5 topic starters for that category and the number of topics they started

And immediately it gave back (this is a non-discourse DB):

```sql
WITH TopTopicStarters AS (
    SELECT
        c.id AS category_id,
        c.name AS category_name,
        u.name AS user_name,
        COUNT(t.id) AS num_topics_started,
        ROW_NUMBER() OVER(PARTITION BY c.id ORDER BY COUNT(t.id) DESC) AS rank
    FROM topics t
    JOIN categories c ON t.category_id = c.id
    JOIN users u ON t.user_id = u.userid
    GROUP BY c.id, u.name
)

SELECT 
    c.category_name,
    tts.user_name,
    tts.num_topics_started
FROM categories c
JOIN TopTopicStarters tts ON c.id = tts.category_id
WHERE tts.rank <= 5;

```

No way could I have come up with that without a lot of googling and trial and error!

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [March 25, 2024, 1:19pm UTC](https://meta.discourse.org/t/rails-console-commands/300682/14 "2024-03-25T13:19:21Z")

</div>

There are many examples in the [data explorer](https://meta.discourse.org/t/32566?silent=true) plugin and category.
