首先建立一个连接文件,当然也可以直接将操作数据库的类直接写在里面
require 'active_record'
ActiveRecord::Base.establish_connection(
  :adapter  => "mysql", 
  :host     => "localhost", 
  :username => "root", 
  :password => "root", 
  :database => "railscode" #我的数据库
)

model类
require 'activeRecord_Base.rb'

class Product < ActiveRecord::Base
  set_table_name :product
end

product=Product.find(1)
print product.product_name

这样就能直接在sciTE里面直接测试数据库连接,实验起来也比较方便
假如你不知道base.rb里面的find方法到底是什么意思,也可以直接实验,方法直接进base.rb里面找
def find(*args)
        options = extract_options_from_args!(args)
        validate_find_options(options)
        set_readonly_option!(options)

        case args.first
          when :first then find_initial(options)
          when :all   then find_every(options)
          else             find_from_ids(args, options)
        end
      end


方法的参数是一个"*",这样应该也能知道了,很方便的能看的懂的例子。源代码里面也有作者写的测试和demo。
这样学习相信也能增强对ruby代码的阅读能力了,呵呵。
个人学习的一些方法。。。。。。。
适合读rails的源代码(当然,我还是个菜鸟,也只能这样了)
评论
发表评论

提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则

您还没有登录,请登录后发表评论

不是流氓
搜索本博客
最近加入圈子
存档
最新评论