Today I Learned Notes to self about software development

    Moving hidden files in the command line

    I was attempting to move all files from a subfolder into the root folder in the command line and ran:

    mv subfolder/* .
    

    but to my surprise, files beginning with ., like .gitignore, did not move.

    It turns out hidden files are exlcuded by default I guess?

    If you want to move dotfiles, you can do this instead:

    mv subfolder/{,.}* .
    

    See this answer.

    Docker WSL2 - failed to solve with frontend dockerfile.v0

    I was getting this error when I tried building an image:

    : failed to create LLB definition: rpc error: code = Unknown desc = error getting credentials - err: exit status 255, out: ``
    

    Restarting WSL didn’t help, but I found out that disabling the buildkit does fix it.

    DOCKER_BUILDKIT=0 docker build ...
    

    See SO answer.

    Rails migration shortcuts you might not know of!

    These are definitely listed in the Rails guide, but I rarely see anyone use these so I wasn’t aware they existed!

    Specifying modifiers

    Type Modifiers are listed here. Things like, setting default values, null constraints, and character limits. Apparently, you can specify some of them in the generator command.

    Of course, they don’t just TELL you which ones you can use 😩 that would be too easy. After searching for a long while I found the source code and I think these are the only currently supported modifiers:

    • limit: Sets the maximum number of characters for a string column and the maximum number of bytes for string/text/binary/integer columns.
    • precision: Specifies the precision for decimal/numeric/datetime/time columns.
    • scale: Specifies the scale for the decimal and numeric columns, representing the number of digits after the decimal point.
    • polymorphic: When generating with references, this option will create two columns which can be used for polymorphic associations: <column_name>_type and <column_name>_id.

    To specify these modifiers, you need to pass values enclosed in curly braces after the field type like this:

    Limit:

    rails g migration AddNameToUsers name:string{40}
    

    Precision and Scale:

    rails g migrationAddAmountToProducts amount:decimal{10.2}
    

    You must set both precision and scale at once.

    Polymorphic

    rails g migration add_supplier_to_products supplier:references{polymorphic}
    

    Specifying indexes

    A third value can be specified using another : after the column name and type, to configure the index of the column. This could also be the second option if the column type is a String.

    :index: will just add an index

      add_column :products, :amount, :string
      add_index :products, :amount
    

    :uniq: will add unique: true in the migration.

    Creating join tables

    Migration names containing JoinTable will generate join tables for use with has_and_belongs_to_many associations.

    rails g migration CreateJoinTableCustomerProduct customer product
    

    will create the migration:

    def change
      create_join_table :customers, :products do |t|
        # t.index [:customer_id, :product_id]
        # t.index [:product_id, :customer_id]
      end
    end
    

    Nice.

    Docker Login Issue with WSL2

    Try re-authenticating with Docker Desktop and then restart Docker, WSL, and VSCode (which should say something like “updating”).

    Then you should be good to go.

    (trying to log in via CLI in WSL won’t work don’t try)

    • https://github.com/microsoft/WSL/issues/7174

    Passing Variables to View Partials

    Sometimes the multiple ways that variables are passed to partials confuses me.

    locals

    Using locals, whenever you render a partial you must declare the same variable. If you attempt to render the same partial in a different view template without declaring the same local variable, you’ll get an error.

    <%= render :partial => 'form', :locals => { :post => @post } %>
    

    local_assigns

    In cases where sometimes you want to pass a variable to a partial but other times you don’t, use local_assigns. Be aware that you will need to check for the existense of a variable in the partial before you use it.

    <%= render article, full: true %>
    <!-- in template -->
    <h2><%= article.title %></h2>
    
    <% if local_assigns[:full] %>
      <%= simple_format article.body %>
    <% else %>
      <%= truncate article.body %>
    <% end %>
    

    object

    Every partial also has a local variable with the same name as the partial (minus the leading underscore). You can pass an object in to this local variable via the :object option:

    <%= render partial: "customer", object: @new_customer %>
    

    If you see the super shorthand syntax:

    <%= render @customer %>
    

    it uses object: @customer.

    Local Variables in Collections

    These work essentially the same as with single object view partials. To customize the name of the local variable, use the :as option:

    <%= render partial: "product", collection: @products, as: :item %>
    

    You can also still create local variables using locals like before:

    <%= render partial: "product", collection: @products,
               as: :item, locals: {title: "Products Page"} %>
    

    Read more in the Rails Guide.