In 2016/04/10 I start 100 days of code challenge. It was not easy and I almost have done it. Almost it means that one day stays without commit. But happy information is that 99 days was with at least one commit. I was a very busy time for me. I learn a lot, especially how to code in Python. But this one-day caused that this whole challenge it does not count. So from today I start again nex, the second edition 100 days of code challenge. Rules are the same.
As we can see in above picture there is the screenshot from my GitHub account from today.
In last few hours, I was creating some application that could work in near future as a blog. But before that good to make some nice structure to have a base of application. So this post is about: how to create a blog using scaffolding. Scaffolding is a quick way to generate some of the most important pieces of an application. If I want to create models, view and also controllers for a new resource in a single operation scaffolding is a great way to make the job quick and easier.
Creating a new rails app is very easy. To do that, I can type only:
rails new scaffold_app
The whole structure of app is as:
To create an MVC components needed to post comments in the scaffold app directory we need to use scaffold generator:
rails generate scaffold post title:string body:text
And this generator creates post:
rails generate scaffold comment post_id:integer body:text
Creates post and comments tables in database:
Rails uses rake commands to run migrations:
Rake routes are the lists of all the URLs currently uses in the application.
After running rails server we can see that some very simple post and comments formulas were created. Good idea is to add some user with some unique id – integer type, public name – string type, email address – string type that will be used as a username. id, name, email attributes are columns in users table in a database.
To user should be associated each post that user will create:
rails generate scaffold Micropost content:string user_id:integer
So, for now, we should have scaffold application with:
- and adding each of them to database.
A few day ago I was struggling with some strange (for me as a beginner) problem with PostgreSQL database during generate some application skeleton. Running some: rails new app —database=postgresql and after that: rake db:migrate or rake db:create cause the fallowing problem:
I restart also database but the problem still appears. So I start to looking for some answer. First I check if my database it’s running, to perform some simple check:
ps auxww | grep postrges
This simple command should display all processes that are running and also connected with postres, but of corse, in my case, output was totally different. PostgreSQL was not running at all:
andrzejdubaj 19418 0,0 0,0 2442020 896 s003 S+ 12:43 0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn postgres
I have PostgreSQL data files from a previous version of PostgreSQL and they are not compatible with current one. One solution was to delete all data and get a fresh database, basically to wipe data from PostrgreSQL completely including the user and data files.
rm -rf /usr/local/var/postgres && initdb /usr/local/var/postgres -E utf8
from my rails application to get setup again. Next step was to check if my changes work as intended:
First process that is displayed gere is a masterserver process. The command shown for it are the same ones given when it was launched. Next two ones are the bacground workers processes launched automatically by the master process. Each of the remaining processes is a server process handling one client connection.
So I we can see this simple solution resolve problem with database on version 9.5.
Today I have struggled with one exercise from exercism.io. In the first look exercise was very simple: ‘Write a function that greets the user by name, or by saying “Hello, World!” if no name is given. For an input of “Alice”, the response should be “Hello, Alice!”. If a name is not given, the response should be “Hello, World!”‘.
On the first look, this task was very simple. To create some function that takes some argument instance variable. Below I passed the first resolution:
def self.hello(name = "World")
@name = name
This solution is ok, but this is one little thing that can be changed to a proper more elegant solution. But first let thing what for is this line:
@name = name
This line is instance variable and becomes class variable. Is this particular line is needed? I was thinking that yes, this line is needed, for me, this notation was more readable. But I was in mistake. This piece of code is useless. I can remove this line and execution this simple program will not change. Why?
that is in:
is interpolated inside the string, to get their value and not just referenced by their name to print on the screen. To wrap variable we can use:
So there is no need to create additional value and the final program code looks as follows:
def self.hello(name = "World")
Above code looks nicer that before. But why as a developer I should use string interpolation and what for? This code is very simple and short and probably someone can say why to use this type of construction? Let’s imagine some longer string that contains more than 5 or 6, maybe 122, or more variables. What then? Probably is more convenient to use this kind of string structure to make life easier. And the second thing: for me, the syntax is more readable and of a first look, it’s easier to figure out what’s going on in the code.