mysql,Linux,HighPerformance,ruby on Rails

显示标签为“rails”的博文。显示所有博文
显示标签为“rails”的博文。显示所有博文

2010年1月8日星期五

rails中实现Schemaless 数据的plugin,基于mysql

说话,如果你读到dbanotes关于FriendFeed使用Mysql的博文<FriendFeed 使用 MySQL 的经验>,肯定会知道
FriendFeed巧妙的使用原生Mysql的特性来实现"Schema-less",说白点就是使用一个字段来存一个实体属性集合。
它使用binary格式存储。
上面说了这些,你如果想使用rails来实现,现推荐此rails plugin Dynamic Attributes
就是按上面的思路实现的,挺好,实用,去下一个吧。

另外,对于windows上的一键ruby环境安装,我用virtualBox安装上windows测试了一款日本货,不错,开源的。
rumix,在中文winXP下安装测试,没有问题。vista,win7就不要上了,听其它网友安装上,有点小问题。点击下载吧
Rumix 1.00(通常版)
--
tommy xiao
E-mail: xiaods(AT)gmail.com

2010年1月3日星期日

新年第一推,快速创建rails3应用

今天有空读了一篇Yehada Katz的博文, Spinning up a new Rails app。正好合适新年的第一推,所以上来发一下。
目的很明确,就是快速创建rails3项目,按如下操作即可:

Step 1: Check out Rails
$ git clone git://github.com/rails/rails.git

Step 2: Generate a new app

$ ruby rails/railties/bin/rails new_app
$ cd new_app

Step 3: Edit the app's Gemfile

# Add to the top directory "/path/to/rails", :glob => "{*/,}*.gemspec" git "git://github.com/rails/arel.git" git "git://github.com/rails/rack.git"

Step 4: Bundle

$ gem bundle

注意,这里需要先安装gem install bundler,才可以。

最后

Everything should now work: script/server, script/console, etc.如果实在想搞自动化的,需要脚本的朋友,可以看看这里



收工。祝新年好运!

--
tommy xiao
E-mail: xiaods(AT)gmail.com

2009年12月29日星期二

Windows下使用rails遇到的问题收集-episode#1

1.连mysql遇到Mysql::Error: query: not connected: CREATE TABLE `schema_migrations` (`version`
varchar(255) NOT NULL) ENGINE=InnoDB
原因是ruby gem中 Mysql Client Library(libmySQL.dll)高版本不支持.下载一个放到\bin\下就直接能用.地址: http://instantrails.rubyforge.org/svn/trunk/InstantRails-win/InstantRails/mysql/bin/libmySQL.dll .

2.window下最好的rails editor是什么,是VIM.
Vim configuration需要配,有详细说明是最理想的,这里

rails.vim的常用指令:

:Rails {目录}
在指定的目录建立一个新的Rails应用程式。如 :Rails d:\depot

:Rake {指令}
直接呼叫Rake进行指定的动作。如 :Rake db:migrate

:Rgen {指令}
呼叫script/generate。如 :Rgen controller admin

:Rserver
呼叫script/server。

:Rproject
在画面的左端以树状结构显示Rails程式目录

:Rpreview {名称}
打开浏览器检视指定的位址。例如输入:Rpreview store 就会打开http://127.0.0.0:3000/store/

:Redit {名称}
直接打开指定的档名。 (注:有些档案不能用这种方式开启。)

:Rcontroller {名称}
:Rmodel {名称}
:Rview {名称}
:Rlayout {名称}
打开指定的controller / model / view / layout
接下来有几个指令一定要特别提出来讲。这是rails.vim最强大的部份:
1. gf

gf这个指令根据游标所在的位置不同会有不同的效果,直接看例子(*代表游标的位置)

游标的位置 打开的档案
Pos*t.find(:first) app/models/post.rb
has_many :c*omments app/models/comment.rb
:bl*og app/controllers/blog_controller.rb" onmouseover="this.style.backgroundColor='#ebeff9'" onmouseout="this.style.backgroundColor='#fff'">link_to “Home”, :controller => :bl*og app/controllers/blog_controller.rb
'sh*ared/sidebar' app/views/shared/_sidebar.rhtml" onmouseover="this.style.backgroundColor='#ebeff9'" onmouseout="this.style.backgroundColor='#fff'">= render :partial => 'sh*ared/sidebar' app/views/shared/_sidebar.rhtml
%= stylesheet_link_tag :scaf*fold public/stylesheets/scaffold.css

甚至,假设我们有个admin_controller 中有一段程式码:

def s*ay

end

输入gf就会马上切换到say对应的view (app/views/admin/say.rhtml)
2. :R跟:A

这两个指令也是在不同的档案间切换用的,运作方式如下:
目前的档案 输入:A会切换到 输入:R会切换到
model 对应的unit test 对应的migration
controller (in method) 对应的functional test 对应的template (view)
template (view) 对应的helper 对应的controller中method的位置
migration 前一版的migration 下一版的next migration
















参考:http://devpoga.wordpress.com/2008/02/05/vim-rails/

最后发现国人制作的vimmate,更加方便,快速部署.

2009年10月29日星期四

在rails中attr_accessible是Mass Assignment exploit的救星

查railscast一则教程,写到使用curl可以方便模拟一个包来hack

curl -X PUT可以设置HTTP 命令

curl -d可以POST数据

attr_protected有如鸡肋,在has many下竟然可以级联update其它属性.直接使用attr_accessible就可以指定赋值对象了.

所以在model中要使用attr_accessible,限制更新字段.

2009年10月14日星期三

关于CustomValidates的使用demo,及一个ruby中的The splat operator.

CustomValidates,源自看advanced rails recipes里的recipe 10:
Write Your Own Custom Validations .
书中使用的rails版本已经很老,所以在rails 2.3下使用书中讲的过不了。
参考这里:
http://chrisblunt.com/blog/2009/04/18/rails-writing-dry-custom-validators/
把validates放在config/initializers里,不需要在environment.rb里加require就OK了。
直接按书中写test unit,直接通过。:-)
然后涉及到给class里加mothod的方法,看这里
http://blog.codevader.com/2008/09/27/including-and-extending-modules-in-ruby/
讲了一遍,我看了遍,记了一下。
然后又涉及到一个 *号,splat operator,
看这里
http://theplana.wordpress.com/2007/03/03/ruby-idioms-the-splat-operator/
注意一下comments里的一个例子:
It may also help to note that ’splat’ returns a copy of the instance that it’s used with, whereas not using ’splat’ returns a reference to the instance.
说的就是加*的是返回一个实例的copy,不加*的是返回一个实例的reference,下面代码一目了然。

Ex.\
[
>group = ["blue", "green", "pink"]
>painting = ["paintbrush", "canvas", group]
>group[2] = “purple”
>puts painting

gives us: paintbrush, canvas, blue, green, purple
]

[
>group = ["blue", "green", "pink"]
>painting = ["paintbrush", "canvas", *group]
>group[2] = “purple”
>puts painting

gives us: paintbrush, canvas, blue, green, pink
]

2009年10月9日星期五

route里的:member和:collection是怎么用的

以下内容基于rails,route里经常会用到collection和member,但已经忘了它是何物了,可见不练的后果。查之,

这里的对话可以直接让我明白,相信你也会明白.
总结下来,想在RESTful的7种action之外再增加其它action,就需要使用:collection,如果想针对单个object作操作,请使用:member.

RubyHunt: What’s the use of the :collection and :member methods within a route? What’s the difference between the two?
melvinram: Collection adds routes to the entire collection
RubyHunt: So the collection is used to add another action inside the controller
melvinram: If you had a resource of Contacts, a collection route you might add would be delete_all_older_than_6_months
melvinram: and the route to get to it would be be /contacts/delete_all_older_than_6_months/
RubyHunt: Which otherwise would be restricted to only 7 actions ?
melvinram: yes
melvinram: A member method applies only to one record. For example, you might add a member method called upgrade_to_vip that would update one record to have a status of VIP or something…
melvinram: And the route would be /contacts/1/upgrade_to_vip
RubyHunt: So the :collection is used to add action inside a RESTful controller and the :member would be used to apply action for individual object.

2009年8月7日星期五

[Rails] alipay商户支付的接口lib纯净版,share.

近日给公司项目写了一个alipay商户支付的接口demo,share.
是从github上某私人项目集成的activemerchant扩展中发现有alipay,就改过来了。
目标是改成一个lib,直接就在rails中使用。
直接下载吧。
git://github.com/xiaods/alipay-lib.git