GithubHelp home page GithubHelp logo

messenger's Introduction

スコープ

使用するメリット

独自の検索機能をメソッドとして切り出すことができる(クラスメソッドとして使用できる)

  • コードの可読性向上
  • 再利用可
  • 仕様変更が容易

送り手と受け手の「組み合わせ」で判定するbetweenスコープ

scope :between, -> (sender_id, recipient_id) do
  where("(conversations.sender_id = ? AND conversations.recipient_id =?) OR (conversations.sender_id = ? AND  conversations.recipient_id =?)", sender_id, recipient_id, recipient_id, sender_id)
end

わかりやすくするためscopeを使わず、OR以降の条件も一旦外して解説します。 なお、ここでは、sender_id=1, recipient_id=2としています。

Conversation.where("(conversations.sender_id = ? AND conversations.recipient_id = ?)", 1,2)

# 上と下の内容は同じです。

Conversation.where("(conversations.sender_id = 1 AND conversations.recipient_id = 2)")

whereメソッド内で「?」を使用することで、「?」に代入するものを切り出すことができます。

さらに、sender_id = 2recipient_id = 1のレコードがあるかも検索する必要があるため、以下のようになる。

Conversation.where("(conversations.sender_id = 1 AND conversations.recipient_id = 2) OR (conversations.sender_id = 2 AND conversations.recipient_id = 1)")

「?」を使用して代入を活用すると

Conversation.where("(conversations.sender_id = ? AND conversations.recipient_id = ?) OR (conversations.sender_id = ? AND conversations.recipient_id = ?)", 1, 2, 2, 1)

最後に「scope」の引数を使用した記述をすることで、元の形になります。

クエリストリング

URLにパラメータを持たせることができることで、viewからcontrollerにパラメータを渡す方法

<%= link_to '以前のメッセージ', '?m=all' %>
# 以下のように書き換えることもできる
<%= link_to '以前のメッセージ', m: 'all' %>
# 以下のようにパラメータを抽出できる
params[:m]
# => 'all'

validates_uniqueness_of

以下は、特定のテーブルに対し、sender_idに対するrecipient_idは一意であるという制約を持たせている。

validates_uniqueness_of :sender_id, scope: :recipient_id

messenger's People

Contributors

kei-kamiguchi avatar

Watchers

 avatar

Forkers

yoyoyoheyheyhey

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.