【问题标题】:Datamapper's analog to find_by_ methodDatamapper 对 find_by_ 方法的模拟
【发布时间】:2012-03-22 22:15:33
【问题描述】:

Datamapper 中是否有类似于 ActiveRecord 的 find_by_ 方法 例如:@user = User.find_by_email(params[:email])

【问题讨论】:

    标签: activerecord sinatra datamapper


    【解决方案1】:

    根据datamapper documentation和我的做法,你可以这样做:

    @user = User.first(:email => params[email]) #strictly the same as find_by_email in Activerecord
    @user = User.all(:email => params[email]) #strictly the same as find_all_by_email in Activerecord
    @user = User.last(:email => params[email]) #no real equivalent in Active Record
    

    【讨论】:

    • 谢谢!如果我理解正确,如果没有记录,它只会返回 nil
    • 如果您正在寻找,您可以选择#first_or_create
    • @user1136228 我记得是的。
    猜你喜欢
    • 1970-01-01
    • 2014-12-28
    • 2011-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多