Ruby on Rails Reference

in #rails5 years ago (edited)

Installation

Guide: https://gorails.com/setup/osx/10.14-mojave

Mojave bug: 'An error occurred while installing mysql2 (0.5.2)`

bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include"

source

New project

rails new MyApp -d mysql --skip-coffee --skip-turbolinks

Migrations

Rails-MySQL integer types

:limit       MySQL-Type   Col Size          Max int (signed)
1            TINYINT      1 byte                         127 (127)
2            SMALLINT     2 bytes                     32,767 (32k)
3            MEDIUMINT    3 bytes                  8,388,607 (8m)
nil, 4, 11   INT(11)      4 bytes              2,147,483,647 (2b)
5..8         BIGINT       8 bytes  9,223,372,036,854,775,807 (9bb)

https://ariejan.net/2009/08/20/once-and-for-all-rails-migrations-integer-limit-option/

Rails-MySQL text types

:limit (bytes)     MySQL-type   Rails-type
    1..255         VARCHAR      :string
    1..255         TINYTEXT     :text
  256..65535       TEXT         :text
65536..16777215    MEDIUMTEXT   :text
     ..4294967295  LONGTEXT     :text

To create a CHAR column:

    t.column :token, "char(12)"

To have a custom id column:

    create_table :posts, :id => false do |t|
      t.integer :id,         null: false, limit: 4, options: 'PRIMARY KEY' # <-- doesnt do anything
    end

Undo

rake db:rollback STEP=1

Emoji and UTF-8

http://blog.arkency.com/2015/05/how-to-store-emoji-in-a-rails-app-with-a-mysql-database/

http://tech.taskrabbit.com/blog/2014/04/24/active-record-mysql-and-emoji/

http://jasonseifer.com/2014/06/20/rails-4-mysql-and-emoji-mysql2error-incorrect-string-value-error

http://metova.com/dev/blog/2015/05/01/add-emoji-support-rails-4-mysql-5-5/

Innodb one file per table

https://stackoverflow.com/questions/3927690/howto-clean-a-mysql-innodb-storage-engine/4056261#4056261

UTF-8 handling

    body
    .encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
    .gsub(/joel/, name)

https://robots.thoughtbot.com/fight-back-utf-8-invalid-byte-sequences


You have middleware in your stack that is forcing this string to UTF-8, even when it is not valid UTF-8. The code that is doing this is bugged.

Observe:

    s = "a=\xff"
    # => "a=\xFF"
    s.force_encoding("binary")
    # => "a=\xFF"
    s.valid_encoding?
    # => true
    Rack::Utils.parse_nested_query(s)
    # => {"a"=>"\xFF"}
    s.force_encoding("utf-8")
    # => "a=\xFF"
    s.valid_encoding?
    # => false
    Rack::Utils.parse_nested_query(s)
    ArgumentError: invalid byte sequence in UTF-8

http://dev.mensfeld.pl/2014/03/rack-argument-error-invalid-byte-sequence-in-utf-8/

DateTime / strftime

    Date (Year, Month, Day):
      %Y - Year with century (can be negative, 4 digits at least)
              -0001, 0000, 1995, 2009, 14292, etc.
      %C - year / 100 (round down.  20 in 2009)
      %y - year % 100 (00..99)

      %m - Month of the year, zero-padded (01..12)
              %_m  blank-padded ( 1..12)
              %-m  no-padded (1..12)
      %B - The full month name (``January'')
              %^B  uppercased (``JANUARY'')
      %b - The abbreviated month name (``Jan'')
              %^b  uppercased (``JAN'')
      %h - Equivalent to %b

      %d - Day of the month, zero-padded (01..31)
              %-d  no-padded (1..31)
      %e - Day of the month, blank-padded ( 1..31)

      %j - Day of the year (001..366)

    Time (Hour, Minute, Second, Subsecond):
      %H - Hour of the day, 24-hour clock, zero-padded (00..23)
      %k - Hour of the day, 24-hour clock, blank-padded ( 0..23)
      %I - Hour of the day, 12-hour clock, zero-padded (01..12)
      %l - Hour of the day, 12-hour clock, blank-padded ( 1..12)
      %P - Meridian indicator, lowercase (``am'' or ``pm'')
      %p - Meridian indicator, uppercase (``AM'' or ``PM'')

      %M - Minute of the hour (00..59)

      %S - Second of the minute (00..59)

      %L - Millisecond of the second (000..999)
      %N - Fractional seconds digits, default is 9 digits (nanosecond)
              %3N  millisecond (3 digits)
              %6N  microsecond (6 digits)
              %9N  nanosecond (9 digits)
              %12N picosecond (12 digits)

    Time zone:
      %z - Time zone as hour and minute offset from UTC (e.g. +0900)
              %:z - hour and minute offset from UTC with a colon (e.g. +09:00)
              %::z - hour, minute and second offset from UTC (e.g. +09:00:00)
              %:::z - hour, minute and second offset from UTC
                                                (e.g. +09, +09:30, +09:30:30)
      %Z - Time zone abbreviation name

    Weekday:
      %A - The full weekday name (``Sunday'')
              %^A  uppercased (``SUNDAY'')
      %a - The abbreviated name (``Sun'')
              %^a  uppercased (``SUN'')
      %u - Day of the week (Monday is 1, 1..7)
      %w - Day of the week (Sunday is 0, 0..6)

    ISO 8601 week-based year and week number:
    The week 1 of YYYY starts with a Monday and includes YYYY-01-04.
    The days in the year before the first week are in the last week of
    the previous year.
      %G - The week-based year
      %g - The last 2 digits of the week-based year (00..99)
      %V - Week number of the week-based year (01..53)

    Week number:
    The week 1 of YYYY starts with a Sunday or Monday (according to %U
    or %W).  The days in the year before the first week are in week 0.
      %U - Week number of the year.  The week starts with Sunday.  (00..53)
      %W - Week number of the year.  The week starts with Monday.  (00..53)

    Seconds since the Unix Epoch:
      %s - Number of seconds since 1970-01-01 00:00:00 UTC.
      %Q - Number of microseconds since 1970-01-01 00:00:00 UTC.

    Literal string:
      %n - Newline character (\n)
      %t - Tab character (\t)
      %% - Literal ``%'' character

    Combination:
      %c - date and time (%a %b %e %T %Y)
      %D - Date (%m/%d/%y)
      %F - The ISO 8601 date format (%Y-%m-%d)
      %v - VMS date (%e-%b-%Y)
      %x - Same as %D
      %X - Same as %T
      %r - 12-hour time (%I:%M:%S %p)
      %R - 24-hour time (%H:%M)
      %T - 24-hour time (%H:%M:%S)
      %+ - date(1) (%a %b %e %H:%M:%S %Z %Y)
Sort:  

Congratulations @gists! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.27
TRX 0.13
JST 0.031
BTC 61905.09
ETH 2902.93
USDT 1.00
SBD 3.55