Translate

Sunday, May 31, 2015

Adding HTML tag in Rails link_to


Rails



Recently ,I've been working on Ruby on Rails and  had to do something  like the example shown below, but using the helper link_to:

Example:

    
 
<a data-method="delete"rel="nofollow" href='sign_out'>
<i class="fa fa-sign-out fa-fw"</i>Logout
</a>


After researching, I found the answer, you should add  your html to the link_to helper as shown below:


Wednesday, May 27, 2015

ERROR 1701 (42000): Cannot truncate a table referenced in a foreign key constraint


If you have ever had this error, is because you want to truncate a table that has foreign key in another table. Let me put it this way.

Having this  structure, where "articles" can have multiple users , and you want to delete the table users using the function TRUNCATE, Mysql will throw this error: ERROR 1701 (42000): Cannot truncate a table referenced in a foreign key constraint


There is a way to avoid this error, but you have to understand,  if you want to delete the  users tables and reset the auto increment field , you have to do the same with the articles table.

This is a basic example, but what would happen if you want to clean an entire database and reset the auto increment? That would be very complicated without the following code.


I know this code could be useful for you:



SYNTAX:
   

     SET foreign_key_checks = 0;

       TRUNCATE  `users`;


       TRUNCATE  `articles`;


     SET foreign_key_checks = 1;

 
Doing that, you will temporarily disabling  referential constraint, and at the end you will enable the referential constraint.

How to Reset auto increment after deleting a table row





To delete data and reset the auto increment field using MYSQL, we have a function called TRUNCATE which will help you to accomplish this task.


Syntax:

   

     TRUNCATE  `articles`;

 
Basically , After the function "TRUNCATE" you have to write your table name , in this case is the table called " ARTICLES" .When you run this SQL sentence you will delete the whole data and the auto increment field will be reset.

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.