Timestamps arent necessary

Whenever we create models in rails, we get a migration where timestamps are added to it. I accepted it as a rle of thumb till a day I was required to wrap my classes around a database in which the tab;es dd not have time stamp fields created_at and updated_at

Well then I tried the following piece of code rails g model tweet content:text and modified its migration so that it looks as floows


class CreateTweets < ActiveRecord::Migration
def change
create_table :tweets do |t|
t.text :content
end
end
end

Look at it, it has no timestamps in it. I ran the migration and tried this code on the rails console


2.0.0-p247 :002 > t = Tweet.new
=> #<Tweet id: nil, content: nil>
2.0.0-p247 :003 > t.content = "Tweet without time"
=> "Tweet without time"
2.0.0-p247 :004 > t.save
(0.3ms) begin transaction
SQL (0.9ms) INSERT INTO "tweets" ("content") VALUES (?) [["content", "Tweet without time"]]
(190.3ms) commit transaction
=> true
2.0.0-p247 :005 > Tweet.all
Tweet Load (0.4ms) SELECT "tweets".* FROM "tweets"
=> #<ActiveRecord::Relation [#<Tweet id: 1, content: "Tweet without time">]>
2.0.0-p247 :006 >

And it worked, proving Timestamps aren’t necessary. But its better to have them

, ,

1 Comment

Parallel assignment in ruby

Saw this code posted by my friend. Thought it was worthy to tell many


Despite being a rubyist for years, I was fooled by this
irb> a = b = c = []
irb> a << 1
=> [1]
irb> b
=> [1]
irb> c
=> [1]
The lesson, be careful with parallel assignment as it assigns an object reference, not a value. So use it to initialize multiple objects with an immutable one.
irb> a = b = c = 5
=> 5
irb> a = 8
=> 8
irb> b
=> 5 # works as expected as 5 is an immutable numeric object

Leave a comment

Free ad space if you donate to RVM

Hello People,

I am very much fond of rvm http://rvm.io/ . All the apps I have deployed runs on it. RVM needs funding so that it can be developed further. I want to hasten the process of funding.

I have written a ruby programming book named ‘I Love Ruby’ thats totally free, you can get it here http://goo.gl/FW6slH (this book will be released on jan 2014 or earlier)

If you donate $2000 to RVM I will grant full page advertisement, half page for $1000 and quarter page ad for $500. If you have donated these amounts you can contact me on mindaslab at gmail dot com so that I can put your ad in it.

The advertisement will stay till Jan 31 2014. It must be designed by you.

Please donate to RVM by going to https://www.bountysource.com/fundraisers/489-rvm-2-0

, , , ,

Leave a comment

This name calling thing

Well, I am a web developer for some long long time and web has changed a lot. A good change is Microsoft is almost out of web. No servers that are known to me run Windows and Internet explorer is dead for good (except few bloody large corporations). This blog is not about that it about another stuff.

There is this name calling thing in web technology which these marketing guys used / use to get more money from client. Long long ago we used a technique where the entire page need not get refreshed, but only part of it does. For this we need to write about 300 lines of code (which fortunately can be reused). Then people started calling it Ajax, and webpages having ajax costs a lot. Now Ajax can be written in 5 lines of code if you wish, thanks to jQuery and all other blah blah Javascript stuff.

Long before the term called “smart phones” was coined, we developers designed pages that can be seen even in mobile devices. This thing is now called responsive design and is promoted as a big thing. I really don’t understand what the fuss is about. When it was difficult to do, none promoted it, when it becomes easy, people give it a name and if they can do the most difficult stuff they project themselves as a big thing.

And there is another thing now called the cloud. Which essentially is cheating. Since we have a computer that have tetrabytes of memory, but memory on our handhelds is only gigabytes, but we are now in a age where tons of data can be streamed to us and we need real time stuff (thats the socio illusionary status society has created). If we have one data or update in one device we need that to be updated in another device too, this called for centralized server to sync things, and thats its. With virtualization we can have banks of computers say in USA, Europe, Australia and configure it in such a way that even if one fails other will take over. This thing too has been done for a long time. Now they call it the Cloud.

What the  &^#!@$$#@$#

, , , , , , , , ,

Leave a comment

Mozilla Persona must catch up

Mozilla Logo

Mozilla is one company that really respects the freedom of internet users. While its browser rival Google Chrome ttys to track users and recommend advertisements and search results that suits them, Mozilla’s firefox tries to respect ones privacy if one wishes to be so.

For long we have this single sin on, that is each website needs to authenticate  user, and it has to make user under go painful task of creating an login account. This process is eased by stuff like openId, Google, Facebook and Twitter’s sine  sign on process. But Google and Facebook track a lot of ones behavior since their business is based on advertisement revenues, which depend on these websites knowing what exactly the user wants, and hence tracking is inevitable and your privacy i compromised.

Mozilla is a non profit company that runs on ones donations and it has put up  a single sign on framework called persona https://developer.mozilla.org/en-US/docs/Mozilla/Persona . Persona does not track a person, compromise ones security  and possibly won’t collaborate with NSA.

So try using it in your website. Any gem for it anyone? It would be nice to to have a compact gem to us it in ones web applications. hopefully it catches up and leads.

, , , ,

Leave a comment

Very Simple Search for Active Records

Okay, you might encounter a need to put very simple search for active record / model in your Rails app and here is it. Put this code in your active record model:


##
# A simple search method
def self.search text
columns = [:name, :roll_no, :address, :city, :pin, :ph]
words = text.downcase.split(/\s+/)
query_array_2 = []
for word in words
query_array = []
for column in columns
query_array << "lower(#{column}) like '%#{word}%'"
end
query_array_2 << query_array.join(' or ')
end
query = query_array_2.collect{|e| "(#{e})"}.join(' and ')
self.where query
end

view raw

search.rb

hosted with ❤ by GitHub

Note this line

columns = [:name, :address, :city, :pin, :ph]

in the code snippet, give your own column names in that array.  This array can contain columns that are string and text type. Lets say you have put these stuff in a class called Person , to search it you need to issue a command like this

Person.search "Karthik chennai"

This stuff works well with Sqlite, Postgre and MySQL. If you have tried this out with other DB, please add it in comments. Bye.

, , ,

Leave a comment

Javascript File API

Javascript File API

Learn how to read and upload files the HTML 5 way

, , ,

Leave a comment

Javascript Learning Trail

Though I hate Javascript for all its imperfectness, lie English which is imperfect but has become an international language, javascript is fast becoming the language of the web. Weather you are a PHP, Perl, Python, Ruby or whatever developer, you need to learn Javascript. This blog explains how to go about learning it.

Learn the basics

First know what javascript is. Goto a website like http://www.w3schools.com/js/ and try practisig Javascript. If you are a good programmer this will suffice, else get this book ‘Eloquent Javascript’ from here http://eloquentjavascript.net/ , read it, and make sure you practice it.

Learn Object Oriented Javascript

Object oriented programming is one of the best invention man has made. Read a book like ‘Pro Javascript Design Patterns‘ , to know how to create reusable code. as long as you are designing small applications, things are okay. But serious web apps need something professional and object oriented so that you can accomplish more by typing less.

Many programmers who are using functional programming simply don’t understand the goodness of object oriented programming(OOP). Please don’t feel lazy to learn OOP, if you fail to learn it, you simply cannot go up the professional ladder above a certain level. Realize that Earth is not flat.

Learn jQuery

jQuery is a wonderful DOM manipulation library written atop Javascript which will reduce lines of code you need to type to develop an application. Its has simply become an unofficial extension of Javascript. So if you are asping to become a good front end developer for web, jQuery is must. You can ger a good book on jQuery like Beginning jQuery to read.

Learn Coffeescript

There are lot of typos that you can make in Javascript, and what if i can say that you can reduce 100 lines of Javascript to 700 lines. Or by just typing 700 lines you can create 1000 lines of Javascript. Coffeescript helps you to achieve that. Learn it by by going here, if you have learned Javascript well, coffee script will be jiffy. Learn coffee script from here http://coffeescript.org/

Well, that’s it. I can just say what is the learning path. If you ask me how to deal with your stupid client or demanding girl friend, look out for some other blog 🙂

Leave a comment

Ditching my Rails Book

Hello People,

Am ditching my Rails book. well, I am in  a full time job now, in a kinda hot situation (not that girls here makes me hot),  but some things makes my blood boil, like peoples insisting on using Windows and Ditching Linux. WHATTTTTTTTTTTT? Ya they say this and that for that. Plus there is ample things here to work on. So till my friends here become a programming Jedi, I wont be free.

So I am ditching my Rails book which I announced here, and am going to Improve my Ruby book which can be downloaded clicking here.

, , , ,

Leave a comment

Planning for a Rails book

ror cover girl

Well, after the success of I Love Ruby( http://is.gd/ilr2013 ), I am planning fora Rails book. You can see its cover above 😉

This book will be released under GNU free documentation license. Time will tell if this book will become a success.

, , , ,

1 Comment