2012-04-26 53 views
0
def add 
    if params[:note_add] == "Add" 
     Note.create({:user_id => @user["id"], :note_type => params[:type], :text => params[:note_text], :lng => params[:lng], :lat => params[:lat]}) 
    end 
    end 

我是Ruby on Rails的新手,我试图从数据库中获取数据。我怎么能这样做?如何从红宝石数据库读取数据

回答

1

如果您使用的关系数据库像MySQL,Postgres或其他您应该使用RoR带来的活动记录。它是一个ORM,提供了很多与DB交互的功能,看起来你已经在使用它了。查询数据请查看指南中的这一页。 http://guides.rubyonrails.org/active_record_querying.html

一些例子

Note.find(1) #get note with id=1 
Note.find_by_note_type("sometype") #gets first found with note_type="sometype" (I think so) 
Note.where("note_type = ?", "sometype") #gets all notes that have note_type="sometype" 
0

Note.find()Note.all

假设你已经创建了Note模型。在控制台中试用rails console