0
我希望能夠使用DataMapper進行動態查詢,以便爲我的Sinatra項目搜索Sqlite數據庫。是否有可能? 到目前爲止,我想出了這樣的努力來獲取在由指定的參數所指定的藝人演唱的歌曲:使用DataMapper進行動態查詢
get '/artists/:name' do
@artist = Artist.get(params[:name])
@songs= Song.all(:artist_name => '#{artist.name}')
slim :show_artists
end
這些都是我的DataMapper類:
configure:development do
DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/3sbase.db")
DataMapper.auto_migrate!
end
class Song
include DataMapper::Resource
property :id, Serial
property :title, String
property :year, Integer
belongs_to :artist
end
class Artist
include DataMapper::Resource
#property :id, Serial
property :name, String, :key=>true
property :age, Integer
has n, :songs
end
DataMapper.finalize
這是我.slim文件
/show_artists.slim
h1= @artist.name
p Age: #{@artist.age}
- if @songs.any?
ul#songs
[email protected] do |song|
p <a href="/songs/#{song.id}">#{song.title} </a>
- else
p There are no songs from this artist/band in the database.
每次if語句返回false,所以我得到了「有從數據庫中該藝術家/樂隊沒有歌曲。」消息,儘管我的數據庫中搜索的歌手有歌曲。
在sinatrarb詢問後谷歌組我建議改變 ** @歌曲= Song.all(:ARTIST_NAME => '#{artist.name}')** 與此行: ** @歌曲= Song.all( :artist => {:name => @ artist.name})** 所以問題解決了,但我不確定它爲什麼應該這樣寫。 –