snipt

Ctrl+h for KB shortcuts
#66098

Ruby

rails > override as_json in model

1
2
3
4
def as_json(options = {})
  super(options.merge(include: :authors,
           except: [:password_digest]))
end
https://snipt.net/embed/b8877bff1091216699f8c1dbb0a6cf46/
https://snipt.net/raw/b8877bff1091216699f8c1dbb0a6cf46/
b8877bff1091216699f8c1dbb0a6cf46
rb
Ruby
6
2014-04-18T06:26:41
True
False
Jun 30, 2013 at 02:54 PM
/api/public/snipt/66098/
rails-override-as_json-in-model
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="k">def</span> <span class="nf">as_json</span><span class="p">(</span><span class="n">options</span> <span class="o">=</span> <span class="p">{})</span> </span><span id="L-2"><a name="L-2"></a> <span class="k">super</span><span class="p">(</span><span class="n">options</span><span class="o">.</span><span class="n">merge</span><span class="p">(</span><span class="kp">include</span><span class="p">:</span> <span class="ss">:authors</span><span class="p">,</span> </span><span id="L-3"><a name="L-3"></a> <span class="ss">except</span><span class="p">:</span> <span class="o">[</span><span class="ss">:password_digest</span><span class="o">]</span><span class="p">))</span> </span><span id="L-4"><a name="L-4"></a><span class="k">end</span> </span></pre></div> </td></tr></table>
rails, ruby
#59485

Ruby

rails rename column

1
2
3
4
5
6
7
# $ rails g migration RenameOldColumnToNewColumn

class FixColumnName < ActiveRecord::Migration
  def change
    rename_column :table_name, :old_column, :new_column
  end
end
https://snipt.net/embed/59feee716999ce05bbdafcc5de5cb6f0/
https://snipt.net/raw/59feee716999ce05bbdafcc5de5cb6f0/
59feee716999ce05bbdafcc5de5cb6f0
rb
Ruby
7
2014-04-15T00:54:56
True
False
Apr 18, 2013 at 02:44 AM
/api/public/snipt/59485/
rails-rename-column
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="c1"># $ rails g migration RenameOldColumnToNewColumn</span> </span><span id="L-2"><a name="L-2"></a> </span><span id="L-3"><a name="L-3"></a><span class="k">class</span> <span class="nc">FixColumnName</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Migration</span> </span><span id="L-4"><a name="L-4"></a> <span class="k">def</span> <span class="nf">change</span> </span><span id="L-5"><a name="L-5"></a> <span class="n">rename_column</span> <span class="ss">:table_name</span><span class="p">,</span> <span class="ss">:old_column</span><span class="p">,</span> <span class="ss">:new_column</span> </span><span id="L-6"><a name="L-6"></a> <span class="k">end</span> </span><span id="L-7"><a name="L-7"></a><span class="k">end</span> </span></pre></div> </td></tr></table>
database, migrate, rails, ruby
#59484

Bash

rails add column to table

$ rails generate migration AddEmailColumnToUsers email:string
https://snipt.net/embed/486ebd44a665eb3ca87e8157e4987864/
https://snipt.net/raw/486ebd44a665eb3ca87e8157e4987864/
486ebd44a665eb3ca87e8157e4987864
bash
Bash
1
2014-04-16T18:17:13
True
False
Apr 18, 2013 at 02:40 AM
/api/public/snipt/59484/
rails-add-column-to-table
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="nv">$ </span>rails generate migration AddEmailColumnToUsers email:string </span></pre></div> </td></tr></table>
bash, database, postgresql, rails, ruby, sqlite
#59472

Bash

osx start redis server

$ redis-server /usr/local/etc/redis.conf
https://snipt.net/embed/5a8283aaea67da224543853b67542f46/
https://snipt.net/raw/5a8283aaea67da224543853b67542f46/
5a8283aaea67da224543853b67542f46
bash
Bash
1
2014-04-15T02:40:47
True
False
/api/public/snipt/59472/
osx-start-redis-server
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="nv">$ </span>redis-server /usr/local/etc/redis.conf </span></pre></div> </td></tr></table>
database, rails, redis, ruby
#59111

Ruby

ruby > sqlite > escape string for insert

# this converts single ' quotes to double '' which sqlite treats as single o__O
mystring.gsub(/\\/, '\&\&').gsub(/'/, "''")
https://snipt.net/embed/b98d4881a5ebe77e80d50bea7b91a262/
https://snipt.net/raw/b98d4881a5ebe77e80d50bea7b91a262/
b98d4881a5ebe77e80d50bea7b91a262
rb
Ruby
3
2014-04-18T05:11:48
True
False
Apr 11, 2013 at 01:00 AM
/api/public/snipt/59111/
ruby-sqlite-escape-string-for-insert
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="c1"># this converts single &#39; quotes to double &#39;&#39; which sqlite treats as single o__O</span> </span><span id="L-2"><a name="L-2"></a><span class="n">mystring</span><span class="o">.</span><span class="n">gsub</span><span class="p">(</span><span class="sr">/\\/</span><span class="p">,</span> <span class="s1">&#39;\&amp;\&amp;&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">gsub</span><span class="p">(</span><span class="sr">/&#39;/</span><span class="p">,</span> <span class="s2">&quot;&#39;&#39;&quot;</span><span class="p">)</span> </span></pre></div> </td></tr></table>
ruby, sqlite, sqlite3
#56292

Bash

specifying ports for rspec and spork

$ rspec --color --drb --drb-port 3011
$ spork -p 3011
https://snipt.net/embed/165933cb5dfcee30a045b6030e5918d8/
https://snipt.net/raw/165933cb5dfcee30a045b6030e5918d8/
165933cb5dfcee30a045b6030e5918d8
bash
Bash
2
2014-04-16T22:57:05
True
False
/api/public/snipt/56292/
specifying-ports-for-rspec-and-spork
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="nv">$ </span>rspec --color --drb --drb-port 3011 </span><span id="L-2"><a name="L-2"></a><span class="nv">$ </span>spork -p 3011 </span></pre></div> </td></tr></table>
rails, rspec, ruby, spork