Translate

Wednesday, May 20, 2015

The most useful Rails 4 commands




These are some useful rails commands:


  • rails -v: Gets the rails version.
  • rails new: Generates the  application skeleton  and base files. (e.g rails new myBlog)
  • rails dbconsole: shows the database that  is currently on your system .
  • rails s or rails server: launches the web server (webrick)
  • rake db:migrate: Runs the migrations files that have not been run.
  • rails generate model: Creates the model and will create a migration file
  • rails generate controller: creates the controller in your application
  • rails generate model your_model_name --skip-migration: Generates the model but will not create the migration file.
  • rails generate migration : creates a migration file.
  • rails d migration your_migration_name:removes the migration file specify
  • rake db:rollback: roll back the most recent migration
  • rake routes:  Displays all routes configured in your rails application

Tuesday, May 19, 2015

MYSQL Save the Last Id Inserted in a Variable







To save the last id inserted in a SQL variable , we need to do this:


INSERT INTO Users (user_name, user_email, user_address, user_city)
VALUES ('bryan.hamilton','Tbha@gmail.com','Av.21','ohio'); 

//we create a new variable called userLastId
 SET @userLastId = LAST_INSERT_ID( );

//now, we can use this varable in another sql sentence

INSERT INTO articles (art_title, art_body, art_user_id, art_created_at)
VALUES ('Geologic time scale','my body ..',@userLastId ,NOW());  

 
Basically, when you save the last id inserted in the variable that value is available to use it in others SQL sentences.

Monday, May 18, 2015

Friday, May 15, 2015

Convert a date into Timestamp in Javscript



To convert a date into timestamp milisecond  in JavaScript we are going to use the function new Date() that JavaScript provide  to deal with dates.

Basically, this is code:


//convert date into a timestamp milisecond
var oDate = new Date('2015','05','12','07','30','00');

//printing the timestamp   
console.log(oDate.getTime());


Explanation:

The function  Date() , receives the following parameters:
new Date(year, month, day, hours, minutes, seconds, milliseconds)

so,the Object oDate(Variable) has a method called getTime, that you can access to convert the date into a timeStamp.


Output:

1434115800000


NOTE: In javascript, the conversion to timestamp is in timestamp millisecond and not timestamp epoch. 


Wednesday, May 13, 2015

Regular Expressions You Should Know(REGEX)


In this section, I will be writing some REGEX that could be helpful  to your  projects.



REGEX #1: Numbers Separated by comma or blank spaces.Example(1,2 ,3  ,4)



   //Numbers Separated by comma or blank spaces
   ^[\d]{1,}(\s*,\s*[\d]{1,})+$
 
 

REGEX #2:  Only number


   //Only Number
   ^(\d)+$
 
 
REGEX #3: Alphanumeric


   //Only Alphanumeric 
   ^(\w)+$
 
 
REGEX #4: Validate URL (either http, https or IP number)

   //Validate URL (either htpp,https or IP number)
   ^(http|https):\/\/([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?$
 
 
REGEX #5: Validate Emails. Example : myUser.test01@gmail.com

   //Validate emails
   ^[\w_.+-]+@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$
 
 

REGEX #6: Validate Hours format

//Validate Hours Format
^[\d]{1,2}:[\d]{1,2}:[\d]{1,2}$



REGEX #7: Validate date format

//Validate date Format
^[\d]{2}\/[\d]{2}\/[\d]{4}$



REGEX #8: Validate IP number format (IPV4)

//Validate IP number format (IPV4)
^[\d]{1,3}.[\d]{1,3}.[\d]{1,3}.[\d]{1,3}$



REGEX #9: Regular expression to limit number of characters to 10

//Limit number of characters to 10
^[a-zA-Z]{1,10}$



REGEX #10: Regular expression: Match Not equal to the string "login"

//Match Not Equal to the string "login"
^(?!login$).+$


Monday, May 11, 2015

How to Delete an element from an array?





In this post, we will answer the question : How to Delete an element from an array?

First of all , we  will create our own array called :$aFruits


  
   $aFruits = array('apple','lemon','grapes','pineapple');
 

Secondly,  if we want to delete the lemon element, we have to use the function called unset and we need to access to  that element by  its  position number.


  
  unset($aFruits[1]);
 

Ouput:
 Array ( [0] => apple [2] => grapes [3] => pineapple )

Wednesday, April 29, 2015

Mysql SQL Error 1093 You can't specify target table



In this post, we will learn how to resolve this "Mysql Error 1093 You can't specify target table"



I was trying to delete some data using  this query:



DELETE  FROM articles
where art_id  in (select a.art_id
                          from articles a
                         where a.art_date <= '2015-05-19'

                        );
 

When I executed this query an error suddenly appeared that was: Mysql Error 1093 you can't specify target table.


What was the problem?
 Basically, the delete statement doesn’t allow to perform a sub-query using the same table.

How can I overcome this?

The solution, that I found was quite easy, is as simple as this: you have to wrap the
sub-query in one more select .

Solution:





DELETE FROM articles
 WHERE art_id in (
     SELECT aid FROM(
      SELECT a.art_id FROM articles a WHERE a.art_date <= '2015-05-19'
  ) as a
 );