This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | # user.rb | |
| # simple | |
| has_many :posts, dependent: :destroy # all posts | |
| has_many :published_posts, -> { where(published: true) }, class_name: "Post" # all published posts | |
| # advanced | |
| has_many :comments, through: :posts, dependent: :destroy | |
| has_many :comments_on_published_posts, through: :published_posts, dependent: :destroy # comments on published posts | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | ddb = Aws::DynamoDB::Client.new({ | |
| access_key_id: '', | |
| secret_access_key: '', | |
| region: '' | |
| }) | |
| # or, if you want to connect to local dynamodb database, | |
| ddb = Aws::DynamoDB::Client.new(endpoint: 'http://localhost:8000') | |
| # suppose table is created with following partition_key and range_key, | |
| # partition_key => "partition_key" | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | class UserMailer < ApplicationMailer | |
| default from: "[email protected]" | |
| def welcome(email, name, message) | |
| @email = email | |
| @name = name | |
| @message = message | |
| mail to: @email, subject: "Welcome to Example." | |
| end | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | UserMailer.welcome("[email protected]", "xyz", "some message").deliver_later | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | class UserMailer < ApplicationMailer | |
| before_action { | |
| @email = params[:email] | |
| @name = params[:name] | |
| @message = params[:message] | |
| } | |
| default to: -> { @email }, | |
| from: -> { "[email protected]" } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | UserMailer.with(email: "[email protected]", name: "xyz", message: "some message").welcome.deliver_later | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <%= form_for(@category) do |f| %> | |
| <% if f.object.errors.any? %> | |
| <div id="error_explanation"> | |
| <h2><%= pluralize(f.object.errors.count, "error") %> prohibited this category from being saved:</h2> | |
| <ul> | |
| <% f.object.errors.full_messages.each do |message| %> | |
| <li><%= message %></li> | |
| <% end %> | |
| </ul> | |
| </div> | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | # we are saving data to category and subcategory models | |
| <%= form_with(model: @category) do |f| %> | |
| # instead, we can simply pass url, along with scopes if necessary | |
| # <%= form_with(scope: :category, url: category_path) %> | |
| <% if f.object.errors.any? %> | |
| <div id="error_explanation"> | |
| <h2><%= pluralize(f.object.errors.count, "error") %> prohibited this category from being saved:</h2> | |
| <ul> | |
| <% f.object.errors.full_messages.each do |message| %> | |
| <li><%= message %></li> | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | class Song | |
| include ActiveModel::Model, ActiveModel::Serialization | |
| attr_accessor :name | |
| attr_accessor :movie_ids | |
| attr_accessor :created_at | |
| attr_accessor :updated_at | |
| def self.migrate_table | |
| migration = Aws::Record::TableMigration.new(self) | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | class User < ApplicationRecord | |
| # rails 4 way of defining virtual attribute | |
| attr_accessor :full_name | |
| # new rails 5 way of defining virtual attributes with datatype and default values | |
| attribute :name, :string, default: -> { "No Name" } | |
| attribute :age, :float, default: 99 | |
| before_save :assign_name | |
| before_save :assign_full_name |