代码之家  ›  专栏  ›  技术社区  ›  Leahcim

Sinatra:用户第一种方法

  •  0
  • Leahcim  · 技术社区  · 12 年前

    为了提高我对Ruby的了解,我正在读一本书,这本书正在用Sinatra复制Twitter。我对作者使用

    User.first(:nickname => params[:recipient])
    

    他在整个代码中的多个位置使用它,如下例所示。

    post '/message/send' do
    recipient = User.first(:nickname => params[:recipient])
    Status.create(:text => params[:message], :user => User.
    get(session[:userid]), :recipient => recipient)
    redirect '/messages/sent'
    end
    

    “首先”添加到该方法中的内容。例如,它是否正在搜索第一个以昵称作为参数传入的用户:receiver?换句话说,它等同于“查找”吗?

    我应该补充一点,这也让我感到困惑,因为昵称应该是唯一的,所以如果它确实在做这件事,那么就没有理由需要搜索“第一个”。

    使现代化

    作者正在为ORM使用DataMapper

    1 回复  |  直到 12 年前
        1
  •  1
  •   Leahcim    12 年前

    好的,“first”是一个“finds”的数据映射器方法。从文档中

    zoo  = Zoo.first(:name => 'Metro')    # first matching record with the name 'Metro'