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})** 所以问题解决了,但我不确定它为什么应该这样写。 –