Getter and Setter methods in Ruby are gateways to access class instance variables. In this blog post I will explain how to create a class with getter and setter methods.
Let’s explain what a getter and a setter method in Ruby are!
A Getter method is the only way where you can retrieve the value of an instance variable from outside its class. A Setter method is the only way where you can assign a value to an instance variable from outside its class.
Now that we have an idea on what each one is, let’s explain them using an example. …
When a closure is created, it will have three scope chains: its own scope, the outer function scope, and the global scope.
Migration in Rails is responsible for creating and editing database tables. To migrate the database changes in rails, run
$ rails db:migrate.
Rails will only read the new migration files that have never been migrated before. So once you run a migration file, you can’t go back and change it and run it again. You will have to create a new migration to edit your database.
In this blog post, I will show you how to manually create a migration file in Rails; whether it is a whole new table or fixing an existing table.
You will have to create a new file in your
db/migrate folder and you can choose a name such as
01_create_employees.rb. In this file, you will define a method within a class that inherits from Active Record. This method will create the employees' table. Here is a code snippet on how this file will look…
Rails comes with a built-in script called Generator. It can produce many rails items that are necessary in every web/mobile application such as Models, and Migration. In this blog I will be explaining the Model Generator, how to use it, and how to customize it.
Model generator is one of the most common generators in Rails. Here is the command for the simplest one:
rails generate model Test
This command will generate a model called Test “app/models/test.rb” that has no attributes/class variables. The model will look something like this:
class Test < ApplicationRecordend
It will also create an empty migration for the Test Model, so that when you run it, it will create the actual table for this model. The migration file “db/migrate/create_tests.rb” will look similar to…
Rails is one of the best frameworks to create tables because it simplifies the process of creating and connecting these tables. However, in some situations, it requires slightly complicated steps to set up table relations. Let's take a money transfer example on a platform such as Venmo or Paypal. We would have
Payment models/tables. They have a one-to-many relationship. A user has many payments and a payment belongs to two users, a payer, and a payee. So the
Payment would require two references (user IDs) from the user table to be able to recognize the payer from the payee in a payment transaction. …
In my previous blog, I explained how you organize welcoming screens in your React-Native project. In this blog, I will show you how you can navigate from one screen to another by adding a link to each screen.
Let’s begin with setting up straightforward navigation actions, so each screen
allows the user to jump to another as follow:
Remember from the previous blog we are using the
react-navigation-stack package which will carry down a
navigation prop to each of your screens. We will be adding some buttons to direct the user between these screens. …
Welcoming screens are the first user interaction in any mobile/web application. They usually consist of three main screens, Login Screen, Create Account Screen, and Home Screen. In this blog, I will explain how to include these screens in your frontend React Native Project using the react-navigation package. Here are straight forward steps to help you organize these screens:
First, you need to install the required packages in your frontend react-native project. The following command will install the dependencies to create the navigation structure in your application.
npm install @react-navigation/native
You will also need to install
react-native-gesture-handlerthat is used by this navigator.
Active Record in Rails has simplified database table relations. It made it much easier for the backend developers to connect tables together whether it is many-to-many or one to one.
In this blog, I will show you a simple way how to create a many-to-many relationship in your backend rails project. Open your project and follow with me step by step to tackle this complicated challenge in other programming languages and frameworks.
Decide which models that have a many-to-many relationship. For example, the relationship between authors and books. …
If you have never built a mobile application and you wish to do so, then you are in the right place. This blog will show you some easy simple steps to create a simple Hello-World application that runs on your iOS device (iPhone).
There are many tools you can use in your first mobile application such as Expo, Ionic, NativeScripts, and many others. I’m going to be using Expo because it is a great tool that works across platforms (iOS, Android, Web, and even Desktop), therefore, you only need to write your application once. Also, the way you build and run your application is easy and straightforward. …
In one of my previous blogs, I explained the difference between Scopes and Class Methods in Rails. In this blog, I will explain two benefits of using Scopes instead of writing them as regular Class Methods.
Scopes are just methods that get converted internally by Active Record to Class Methods. For instance, a simplified implementation of ‘A’ student scope would look similar to this after it gets converted to a class method:
That's why many developers don’t feel the need to write scopes instead of class methods believing that it is just another way or a syntactic sugar to write a class method. …