0

I am just trying to study about Rails.

Everytime I send the get request through postman, initialize method is called, so I cannot maintain @data Array because @data is initialized at every request. Is there any way to initialize @data once, and let create, update, destroy methods work properly?

class BooksController < ApplicationController
    skip_before_action :verify_authenticity_token

    def initialize
        super
        @data = [
            { title: "Harry Potter", author: "J.K Rowling" },
            { title: "Name of the wind", author: "Patrick Rothfuss" }
        ]
    end

    def index
        render json: @data
    end

    def create
        @data.push(params[:book])
        render json: @data
    end
end
2
  • Yes, you need to save it to a database. An instance variable only applies to that instance of the class, every time you make a new request, you create a new instance of your BooksController class, which has no variables associated with it Commented Apr 9, 2020 at 8:17
  • 1
    Thanks @Mark. I just realized that every request created a new instance. This was just for the purpose of study before I get into the database. Commented Apr 9, 2020 at 8:24

2 Answers 2

2

If you want to persist anything between requests you need to store it somewhere:

  • Database
  • Memory based storage (Redis, Memcached)
  • File system

You can also pass state back and forth between the client and server without actually storing it with:

  • HTTP cookies
  • Query string parameters

Using a class variable is not really going to solve anything. It will only hold the variable as long as the class is held in memory. Every time the class is reloaded it will be reset.

Multi-threading is another huge issue here as Rails servers are commonly multi-threaded and class variables are not thread safe.

Sign up to request clarification or add additional context in comments.

Comments

0

Because I just didn't want to use any database yet, and knew that every request created a new instance of BooksController. To make this code work, the following changes could solve it.

class BooksController < ApplicationController
    skip_before_action :verify_authenticity_token

    @@data = [
        { title: "Harry Potter", author: "J.K Rowling" },
        { title: "Name of the wind", author: "Patrick Rothfuss" }
    ]

    def index
        render json: @@data
    end

    def create
        @@data.push(params[:book])
        render json: @@data
    end
end

2 Comments

This is ok as a learning experience, but what you should take away from this exercise, is that really your data should live outside of your classes. Rails helps you a lot with that, it will actually be easier, if you create an Active Record Model.
Thanks @Roland. I am sure I will do that in a couple of days.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.