以下のテキストを進めていく中で、ブログ記事を絞り込むという項目があり、理解しようと努力したのですが、
ググったり色々と調べても以下のソースコードの太字の部分、スコープのwhere( )内の< >?の意味がわかりませんでした。
テキストには、publishedスコープは下書き状態ではない記事、つまり公開記事と会員限定記事を選び出す。fullスコープは会員が書いたか下書き状態ではない記事を選び出すと書いてありますが、< >?は否定の意味なのでしょうか?
そもそも、太字のreadable_forスコープの部分など、スコープの書き方自体がよくわからないので、スコープの書き方が理解できるサイトやテキストなどあれば教えていただけると助かります。よろしくお願いいたします。
class Entry < ApplicationRecord
belongs_to :author, class_name: "Member", foreign_key: "member_id"
STATUS_VALUES = %w(draft member_only public)
validates :title, presence: true, length: { maximum: 200 }
validates :body, :posted_at, presence: true
validates :status, inclusion: { in: STATUS_VALUES }
scope :common, -> { where(status: "public") }
scope :published, -> { where("status** <> ?**", "draft") }
scope :full, ->(member) {
where("member_id = ? OR status <> ?", member.id, "draft") }
scope :readable_for, ->(member) { member ? full(member) : common }
class << self
def status_text(status)
I18n.t("activerecord.attributes.entry.status_#{status}")
end
def status_options STATUS_VALUES.map { |status| [status_text(status), status] } end
end
end
使用したテキストおよび該当するページ:インプレス出版 改訂4版 基礎 Ruby On Rails 370P
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/03/09 10:05