ruby on rails

安装Ruby1.9.2:

wget http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.2-p0.tar.bz2
tar jxvf ruby-1.9.2-p0.tar.bz2
cd ruby-1.9.2-p0
./configure --prefix=/usr/local/ruby
make
make test
sudo make install

sudo ln -s /usr/local/ruby/bin/ruby /usr/bin/ruby
sudo ln -s /usr/local/ruby/bin/gem /usr/bin/gem
sudo ln -s /usr/local/ruby/bin/irb /usr/bin/irb

设置环境变量:
sudo gedit /etc/environment

使环境变量生效:
source /etc/environment

安装Rails3RC:
gem install rails --pre

如果报错(no such file to load -- zlib (LoadError) )安装zlib:
sudo apt-get install zlib1g-dev
然后到源码目录的ext/zlib下
ruby extconf.rb
make
sudo make install

sudo ln -s /usr/local/ruby/bin/bundle /usr/bin/bundle
sudo ln -s /usr/local/ruby/bin/rake /usr/bin/rake
sudo ln -s /usr/local/ruby/bin/rails /usr/bin/rails

Mysql:

apt-get install mysql-server-5.1 libmysqlclient16-dev

bundle install mysql2

安装openssl:
apt-get install openssl libssl-dev libopenssl-ruby1.9.1

1. cd /ruby_install_dir/ext/openssl
2. ruby extconf.rb
3. make
4. make install

安装Thin服务器:
gem install thin

libmemcached :
apt-get install memcached
apt-get install libevent-dev

wget http://launchpad.net/libmemcached/1.0/0.44/+download/libmemcached-0.44.tar.gz
tar -xzvf libmemcached-0.44.tar.gz
cd libmemcached-0.44
./configure && make && make install

ln -s /usr/local/lib/libmemcached.so.2 /usr/lib/

Ruby Memcached:
gem install memcached



运行示例:

创建项目:
rails new webapp -d mysql


如果有以下错误:no such file to load -- readline
Ruby 1.9 的时候需要安装 readline 的扩展,如果开始安装的时候没有带上它,那么进入Ruby源代码根目录的 ext 里面重新安装以下 readline 的扩展就可以了

cd ext/readline
ruby extconf.rb
make
make install


Rails3 Install :

sudo apt-get update
sudo apt-get install build-essential libssl-dev libncurses5-dev libreadline5 libreadline5-dev zlib1g zlib1g-dev


安装Ruby1.9.1:
mkdir ~/src && cd ~/src
wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.1-p376.tar.gz
tar -zxvf ruby-1.9.1-p376.tar.gz
cd ruby-1.9.1-p376
./configure && make && sudo make install

查询Ruby安装目录:
which ruby

更新gem:
$ sudo gem update --system
sudo gem sources -a http://gemcutter.org

安装Rails:
sudo gem install rails

查询Rails安装目录:
which rails

安装 SQLite:
sudo apt-get install sqlite3 libsqlite3-dev

安装SQLite支持Ruby:
sudo gem install sqlite3-ruby

创建Rails项目:
rails new hello

安装插件:
bundle install

启动Rails:
rails server

创建Controller:
rails generate controller salutation

创建Model:
rails generate model Article

添加column:
add_column :articles, :excerpt, :string

删除column:
remove_column :articles, :excerpt

验证:
validates :title, :presence => true

进入控制台模式:
rails console

打印表列名:
Article.column_names

查看model方法大小:
Article.methods.size

查询记录行数:
Article.count

查询第一条:
Article.first

查询最后一条:
Article.last

排序:
articles = Article.order("published_at")

倒序:
Article.order ('published_at DESC')

条件查询:
Article.where(:title => 'RailsConf').first
Article.where(:title => 'RailsConf').all

更新数据:
article = Article.first
article.update_attributes(:title => "RailsConf2010", :published_at => 1.day.ago)

删除最后一条数据:
article = Article.last
article.destroy
也可以写成 :
Article.last.destroy

删除ID为1的数据:
Article.destroy(1)

删除ID为2和3的数据:
Article.destroy([2,3])

判断ID为数组或单个ID进行删除:
def destroy(id)
if id.is_a?(Array)
id.map { |one_id| destroy(one_id) }
else
find(id).destroy
end
end

删除ID为5和6:
Article.delete([5, 6])

条件删除数据:
Article.delete_all("published_at < '2011-01-01'")

判断是否有错误:
article.errors.any?

打印出错误信息:
article.errors.full_messages

打印数据字体内错误:
article.errors.on(:title)

打印错误数:
article.errors.size

判断操作是否有效:
article.valid?

关系:
has_one
has_many
belongs_to
has_and_belongs_to_many

你可能感兴趣的:(mysql,memcached,ext,Ruby,Rails)