|
| 1 | +# start-named-scope-1 |
| 2 | +class Band |
| 3 | + include Mongoid::Document |
| 4 | + |
| 5 | + field :country, type: String |
| 6 | + field :genres, type: Array |
| 7 | + |
| 8 | + scope :japanese, ->{ where(country: "Japan") } |
| 9 | + scope :rock, ->{ where(:genres.in => [ "rock" ]) } |
| 10 | +end |
| 11 | +# end-named-scope-1 |
| 12 | + |
| 13 | +# start-query-named-scope |
| 14 | +Band.japanese.rock |
| 15 | +# end-query-named-scope |
| 16 | + |
| 17 | +# start-named-scope-2 |
| 18 | +class Band |
| 19 | + include Mongoid::Document |
| 20 | + |
| 21 | + field :name, type: String |
| 22 | + field :country, type: String |
| 23 | + |
| 24 | + scope :based_in, ->(country){ where(country: country) } |
| 25 | +end |
| 26 | +# end-named-scope-2 |
| 27 | + |
| 28 | +# start-query-named-scope-2 |
| 29 | +Band.based_in("Spain") |
| 30 | +# end-query-named-scope-2 |
| 31 | + |
| 32 | +# start-named-scope-3 |
| 33 | +class Band |
| 34 | + include Mongoid::Document |
| 35 | + |
| 36 | + def self.on_tour |
| 37 | + true |
| 38 | + end |
| 39 | + |
| 40 | + scope :on_tour, ->{ where(on_tour: true) } |
| 41 | +end |
| 42 | +# end-named-scope-3 |
| 43 | + |
| 44 | +# start-default-scope-1 |
| 45 | +class Band |
| 46 | + include Mongoid::Document |
| 47 | + |
| 48 | + field :name, type: String |
| 49 | + field :active, type: Boolean |
| 50 | + |
| 51 | + default_scope -> { where(active: true) } |
| 52 | +end |
| 53 | +# end-default-scope-1 |
| 54 | + |
| 55 | +# start-default-scope-2 |
| 56 | +class Band |
| 57 | + include Mongoid::Document |
| 58 | + |
| 59 | + field :name, type: String |
| 60 | + field :on_tour, type: Boolean, default: true |
| 61 | + |
| 62 | + default_scope ->{ where(on_tour: false) } |
| 63 | +end |
| 64 | + |
| 65 | +# Creates a new Band instance in which "on_tour" is "false" |
| 66 | +Band.new |
| 67 | +# end-default-scope-2 |
| 68 | + |
| 69 | +# start-unscoped |
| 70 | +# Inline example |
| 71 | +Band.unscoped.where(name: "Depeche Mode") |
| 72 | + |
| 73 | +# Block example |
| 74 | +Band.unscoped do |
| 75 | + Band.where(name: "Depeche Mode") |
| 76 | +end |
| 77 | +# end-unscoped |
| 78 | + |
| 79 | +# start-scope-association |
| 80 | +class Label |
| 81 | + include Mongoid::Document |
| 82 | + |
| 83 | + field :name, type: String |
| 84 | + |
| 85 | + embeds_many :bands |
| 86 | +end |
| 87 | + |
| 88 | +class Band |
| 89 | + include Mongoid::Document |
| 90 | + |
| 91 | + field :name, type: String |
| 92 | + field :active, default: true |
| 93 | + |
| 94 | + embedded_in :label |
| 95 | + default_scope ->{ where(active: true) } |
| 96 | +end |
| 97 | +# end-scope-association |
| 98 | + |
| 99 | +# start-scope-association-steps |
| 100 | +label = Label.new(name: "Hello World Records") |
| 101 | +band = Band.new(name: "Ghost Mountain") |
| 102 | +label.bands.push(band) |
| 103 | +label.bands # Displays the Band because "active" is "true" |
| 104 | +band.update_attribute(:active, false) # Updates "active" to "false" |
| 105 | + |
| 106 | +# Displays the "Ghost Mountain" band |
| 107 | +label.bands # => {"_id":"...","name":"Ghost Mountain",...} |
| 108 | + |
| 109 | +# Won't display "Ghost Mountain" band after reloading |
| 110 | +label.reload.bands # => nil |
| 111 | +# end-scope-association-steps |
| 112 | + |
| 113 | +# start-scope-query-behavior |
| 114 | +class Band |
| 115 | + include Mongoid::Document |
| 116 | + |
| 117 | + field :name |
| 118 | + field :touring |
| 119 | + field :member_count |
| 120 | + |
| 121 | + default_scope ->{ where(touring: true) } |
| 122 | +end |
| 123 | + |
| 124 | +# Combines the condition to the default scope with "and" |
| 125 | +Band.where(name: 'Infected Mushroom') |
| 126 | +# Interpreted query: |
| 127 | +# {"touring"=>true, "name"=>"Infected Mushroom"} |
| 128 | + |
| 129 | +# Combines the first condition to the default scope with "and" |
| 130 | +Band.where(name: 'Infected Mushroom').or(member_count: 3) |
| 131 | +# Interpreted query: |
| 132 | +# {"$or"=>[{"touring"=>true, "name"=>"Infected Mushroom"}, {"member_count"=>3}]} |
| 133 | + |
| 134 | +# Combines the condition to the default scope with "or" |
| 135 | +Band.or(member_count: 3) |
| 136 | +# Interpreted query: |
| 137 | +# {"$or"=>[{"touring"=>true}, {"member_count"=>3}]} |
| 138 | +# end-scope-query-behavior |
| 139 | + |
| 140 | +# start-override-scope |
| 141 | +class Band |
| 142 | + include Mongoid::Document |
| 143 | + |
| 144 | + field :country, type: String |
| 145 | + field :genres, type: Array |
| 146 | + |
| 147 | + scope :mexican, ->{ where(country: "Mexico") } |
| 148 | +end |
| 149 | +# end-override-scope |
| 150 | + |
| 151 | +# start-override-scope-block |
| 152 | +Band.with_scope(Band.mexican) do |
| 153 | + Band.all |
| 154 | +end |
| 155 | +# end-override-scope-block |
| 156 | + |
| 157 | +# start-class-methods |
| 158 | +class Band |
| 159 | + include Mongoid::Document |
| 160 | + |
| 161 | + field :name, type: String |
| 162 | + field :touring, type: Boolean, default: true |
| 163 | + |
| 164 | + def self.touring |
| 165 | + where(touring: true) |
| 166 | + end |
| 167 | +end |
| 168 | + |
| 169 | +Band.touring |
| 170 | +# end-class-methods |
0 commit comments