2012-04-26 69 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