Friday, March 28, 2014

Rails Default Scope Affects Model Initialization

Hi Guys - Ran into this while working on a project.  Now, default scope works fine for most of the stuff but it also has a secret to itself which I didn't realize (my bad).

Lets consider two cases,  one in which default scope affect is realized and other one in which it isn't.

------------
class ModelNotAffected < ActiveRecord::Base
  attr_accessible :name, :is_deleted

  default_scope where(:is_deleted => 0)
end

------------

class ModelAffected < ActiveRecord::Base
  attr_accessible :stall, :is_active

  default_scope where(:is_active => 1)
end

------------

How ?

ModelNotAffected.new({:name => 'Spider Man'})
# => {:name => 'Spider Man', :is_deleted => 0}
# This is what u kind of want

ModelAffected.new({:stall => Art Works''})
# => {:stall => 'Art Works', :is_active => 1}
# This might not be the case. There might be some condition which would make the stall as is_active

Well note that how default scope affects model initialization.

Thanks

Thursday, March 20, 2014

Nodejs Watching a file + Spawning new processes to list file information

Hi guys - Trying to get my hands dirty with Node.js and its pretty cool.

Its so easy to do stuff. Here is a small code snippet which demonstrate watching a file and spawning a child process to list the file info.



Thanks


Tuesday, March 18, 2014

Truncating text or string in Ruby

Hi Guys - Today I was working on a small issue where I had to add some client side validation.

The fix was to add limit to the number of chars in a text area. The fix is as easy as adding 'maxlength=n' attribute.

But this can be circumvented easily in the client side. So what ?

In the server side, are we going to throw an error and raise an exception that the text is too long ?

Well another way is to truncate this text.

In ruby, simply do this -



This will simply truncate the text

Thanks

Sunday, March 16, 2014

https curl POST, GET, DELETE Images or Video files

Hi guys,

So I was trying to test some api's from my terminal and I decided to use CURL for doing POST, GET and DELETE.

Now, the api's use HTTPS so I had to figure out how to do https curl. After Googling for few minutes, I was able to figure it out.

There were few things which were missing from most of the posts. Hopefully this will help someone or save someone's time.

Few points to note

#1 - Url is in double quotes
#2 - query string is encoded.

# upload image/video using POST
curl -i --trace -H --data-binary "@/some/path/to/video/or/image/file" "https://somebaseurl/action?encodedQueryString" -v

#get file using GET
curl -i --trace -H -G "https://somebaseurl/action?encodedQueryString"


# delete file
curl -i --trace -H -X DELETE "https://somebaseurl/action?encodedQueryString"

Thanks