Feeds:
Posts
Comments

Following example gives the best undertanding of it. Also refer; http://www.arthurneil.com/ruby-on-rails/difference-include-extend-ruby

module Foo
def a
puts “a: I am a #{self.class.name}”
end

def Foo.b
puts “b: I am a #{self.class.name}”
end

def self.c
puts “c: I am a #{self.class.name}”
end
end

class Bar
include Foo

def try_it
a
Foo.b # Bar.b undefined
Foo.c # Bar.c undefined
end
end

Bar.new.try_it
#>> a: I am a Bar
#>> b: I am a Module
#>> c: I am a Module

include : mixes in specified module methods as instance methods in the target class
extend : mixes in specified module methods as class methods in the target class

module ReusableModule
def module_method
puts “Module Method: Hi there!”
end
end

class ClassThatIncludes
include ReusableModule
end
class ClassThatExtends
extend ReusableModule
end

puts “Include”
ClassThatIncludes.new.module_method       # “Module Method: Hi there!”
puts “Extend”
ClassThatExtends.module_method              # “Module Method: Hi there!”

We can create classes inside another classes in Ruby. In Ruby, classes defined are stored as constants with same name as class. That means any classes (say B,C) defined inside another class (A) would act like constants of that outer class ( as A::B, A::C).

class A
def test_method
“I AM INSIDE CLASS A”
end
class B
def test_method
“I AM INSIDE CLASS B”
end
end
end

p A.new.test_method –> “I AM INSIDE CLASS A”
p A::B.new.test_method –> “I AM INSIDE CLASS B”

Define the custom_databse details in database.yml.

For example;

custom_database:

adapter: mysql
username: root
password:
database: custom_development

All we need to do is to tell which models to use which connection.

(E.g)

class Results < ActiveRecord::Base
establish_connection :custom_database
end

Package versions may not be the latest one. These are the steps which I followed to set up my environment. You may want to download the latest source from web and then build it instead of using the download links mentioned here.

sudo apt-get install build-essential

sudo apt-get install openssl libssl-dev libmysqlclient-dev

sudo apt-get install libcurl3-openssl-dev

#RUBY

sudo wget ftp://ftp.ruby-lang.org//pub/ruby/1.8/ruby-1.8.7-p302.tar.gz

sudo tar -xzvf ruby-1.8.7-p302.tar.gz

cd ruby-1.8.7-p302

sudo ./configure

sudo make

sudo make install

#RubyGems

wget http://rubyforge.org/frs/download.php/70696/rubygems-1.3.7.tgz

tar -xzvf rubygems-1.3.7.tgz

cd rubygems-1.3.7

ruby setup.rb

gem list #To verify

#Rails

gem install rails –include-dependencies –no-rdoc –no-ri

#Thin

gem install thin –no-rdoc –no-ri

thin install

/usr/sbin/update-rc.d -f thin defaults

#nokogiri

Dependencies: libxml and libxslt

libxml:

Shoot the below command to find the libxml development version.

sudo apt-cache search libxml | grep dev

libxml2-dev – Development files for the GNOME XML library.

Install it

sudo apt-get install libxml2-dev

libxslt:

Shoot the below command to find the libxslt development version.

sudo apt-cache search libxslt  libxslt1.1  libxslt1-dev

Install these two

sudo apt-get install libxslt1.1 libxslt1-dev

sudo  gem install nokogiri –no-rdoc –no-ri

#NGINX

apt-get install libpcre3-dev

wget http://nginx.org/download/nginx-0.7.67.tar.gz

tar -xzvf nginx-0.7.67.tar.gz

cd nginx-0.7.67

./configure –with-http_stub_status_module

make

make install

#PHP

cd ..

wget http://monkey.org/~provos/libevent-1.4.14b-stable.tar.gz

tar -xzvf libevent-1.4.14b-stable.tar.gz

cd libevent-1.4.14b-stable

./configure

make

make install

cd ..

wget http://in2.php.net/get/php-5.3.3.tar.gz/from/us.php.net/mirror –   this has problem and saves the mirror, so download it manually and then proceed.

tar -xzvf php-5.3.3.tar.gz

cd php-5.3.3

./configure –enable-fastcgi –enable-fpm –with-mysql –with-gettext –with-openssl –with-curl

make

make install

#ZLIB

cd ..

wget http://zlib.net/zlib-1.2.5.tar.gz

tar -xzvf zlib-1.2.5.tar.gz

cd zlib-1.2.5

./configure

make

make install

cd ..

gem install aws-s3 –no-rdoc –no-ri

gem install json –no-rdoc –no-ri

apt-get install libxslt-dev

gem install oauth –no-rdoc –no-ri

gem install mysql –no-rdoc –no-ri

gem install twitter-auth –no-rdoc –no-ri

apt-get install libcurl4-gnutls-dev

gem install curl-multi –no-rdoc –no-ri

gem install typhoeus –no-rdoc –no-ri

#Subversion

apt-get install subversion

#Rmagick

Download ImageMagick source

tar -xvzf ImageMagick-6.6.4-7.tar.gz

cd  ImageMagick-6.6.4-7

./configure

make

make install

cd ..

gem install rmagick

Installing JVM on ubuntu10.04

sudo add-apt-repository “deb http://archive.canonical.com/ lucid partner”

sudo apt-get update

sudo apt-get install sun-java6-jdk

Basic rule is;

If you don’t need to do anything with the relationship model, it may be simpler to set up a has_and_belongs_to_many relationship.

You should use has_many :through if you need validations, callbacks, or extra attributes on the join model.

For example;

Stories can belong to many categories. Categories can have many stories.

For this we will have;

Categories_Stories Table
story_id | category_id

has_many through gives you a third model which can be used to story various other pieces of information which don’t belong to either of the original models.

For example

Person can subscribe to many magazines. Magazines can have many subscribers.

Thus, we can have a subscription model in the middle, which gives us a similar table to the earlier example, but with additional properties.

Subscriptions Table
person_id | magazine_id | subscription_type | subscription_length | subscription_date

And so on.

 

new creates a blank new instance, filling attributes with the passed params. It is only used on a Model class:

@post = Post.new(attributes)

build is only used on association proxies, not on classes, and it does the same as new, but it also sets the foreign key to the id of the parent element:

@comment = @post.comments.build(attributes_for_comment)

Here @comment will have the attributes_for_comment and the id of @post(post_id)

so we need not do something like;

@comment.post_id = @post.id

If you are using select (or similar helpers such as collection_select, select_tag) to set a belongs_to association you must pass the name of the foreign key (in the example below blog_id), not the name of association itself. If you specify blog instead of blog_id Active Record will raise an error along the lines of ActiveRecord::AssociationTypeMismatch: Blog(#) expected, got String(#)

<%= f.select :blog_id, options_from_collection_for_select(@blogs, ‘id’, ‘name’), :selected => :blog_id, :include_blank => true %>

 

Try;

f.select

not

f.select_tag

That should work.

To login;

> mysql -u username -p

Create a database;

> create database [databasename];

List all databases;

> show databases;

Switch to a particular database.

> use [db name];

To see all the tables in the db.

> show tables;

To see tables property/column details;

> describe [table name];

To delete a db;

> drop database [database name];

To delete a table;

> drop table [table name];

Show all data in a table.

> SELECT * FROM [table name];

Returns the columns and column information pertaining to the designated table.

> show columns from [table name];

Show unique records.

> SELECT DISTINCT [column name] FROM [table name];

Show selected records sorted in an ascending (asc) or descending (desc).

> SELECT [col1],[col2] FROM [table name] ORDER BY [col2] DESC;

Return number of rows.

> SELECT COUNT(*) FROM [table name];

Sum column.

> SELECT SUM(*) FROM [table name];

Creating a new user. Login as root. Switch to the MySQL db. Make the user. Update privs.

# mysql -u root -p

> use mysql;
> INSERT INTO user (Host,User,Password) VALUES(‘%’,’username’,PASSWORD(‘password’));

> flush privileges;

Change a users password from unix shell.
# [mysql dir]/bin/mysqladmin -u username -h hostname.blah.org -p password ‘new-password’

Change a users password from MySQL prompt. Login as root. Set the password. Update privs.

# mysql -u root -p
> SET PASSWORD FOR ‘user’@’hostname’ = PASSWORD(‘passwordhere’);
> flush privileges;

Set a root password if there is on root password.

# mysqladmin -u root password newpassword

Update a root password.

# mysqladmin -u root -p oldpassword newpassword

Give user privilages for a db. Login as root. Switch to the MySQL db. Grant privs. Update privs.

# mysql -u root -p
> use mysql;
> INSERT INTO user (Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv) VALUES (‘%’,’databasename’,’username’,’Y’,’Y’,’Y’,’Y’,’Y’,’N’);
> flush privileges;

or

> grant all privileges on databasename.* to username@localhost;
> flush privileges;

To update info already in a table.

> UPDATE [table name] SET Select_priv = ‘Y’,Insert_priv = ‘Y’,Update_priv = ‘Y’ where [field name] = ‘user’;

Delete a row(s) from a table.

> DELETE from [table name] where [field name] = ‘whatever’;

Update database permissions/privilages.

> flush privileges;

Delete a column.

> alter table [table name] drop column [column name];

Add a new column to db.

> alter table [table name] add column [new column name] varchar (20);

Change column name.

> alter table [table name] change [old column name] [new column name] varchar (50);

Make a unique column so you get no dupes.

> alter table [table name] add unique ([column name]);

Make a column bigger.

> alter table [table name] modify [column name] VARCHAR(3);

Delete unique from table.

> alter table [table name] drop index [colmn name];

Load a CSV file into a table.

> LOAD DATA INFILE ‘/tmp/filename.csv’ replace INTO TABLE [table name] FIELDS TERMINATED BY ‘,’ LINES TERMINATED BY ‘\n’(field1,field2,field3);

Dump all databases for backup. Backup file is sql commands to recreate all db’s.

# [mysql dir]/bin/mysqldump -u root -ppassword –opt >/tmp/alldatabases.sql

Dump one database for backup.

# [mysql dir]/bin/mysqldump -u username -ppassword –-databases databasename >/tmp/databasename.sql

Dump a table from a database.

# [mysql dir]/bin/mysqldump -c -u username -ppassword databasename tablename > /tmp/databasename.tablename.sql

Restore database (or database table) from backup.

# [mysql dir]/bin/mysql -u username -ppassword databasename < /tmp/databasename.sql

« Newer Posts - Older Posts »