Rails console commands

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

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.

1 Like

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

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

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).

8 Likes

Rails console speaks ruby.

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

1 Like

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)

1 Like

You could do something like:

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:

t1 = Topic.find(123)
t2 = Topic.find(124)
t1.update(title: t2.title)
3 Likes

Me too: About this Book :: Why's (Poignant) Guide to Ruby

5 Likes

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.

4 Likes

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

1 Like

Sql is a relic from the 1960s. :stuck_out_tongue_winking_eye:

1 Like

ā€œNicheā€ pays bills!

3 Likes

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

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. :rofl:

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

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):

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!

There are many examples in the data explorer plugin and category.

2 Likes