Skip to content

Instantly share code, notes, and snippets.

View 8parth's full-sized avatar

Parth Modi 8parth

View GitHub Profile
@8parth
8parth / has_many_through_notes.rb
Created January 23, 2017 10:44
has_many :through association with specific conditions
# 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
@8parth
8parth / dynamodb_queries.rb
Last active March 8, 2017 18:23
dynamodb query examples
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"
@8parth
8parth / user_mailer_in_rails_4.rb
Created March 23, 2017 05:46
user mailer in rails 4
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
UserMailer.welcome("[email protected]", "xyz", "some message").deliver_later
@8parth
8parth / user_mailer_in_rails_5.rb
Created March 23, 2017 05:54
new parameterized mailers example
class UserMailer < ApplicationMailer
before_action {
@email = params[:email]
@name = params[:name]
@message = params[:message]
}
default to: -> { @email },
from: -> { "[email protected]" }
UserMailer.with(email: "[email protected]", name: "xyz", message: "some message").welcome.deliver_later
<%= 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>
# 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>
@8parth
8parth / Song.rb
Created March 23, 2017 10:59
Example of has_many with dynamodb
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)
@8parth
8parth / attribute-api-example.rb
Last active March 29, 2017 17:07
Rails 5 Attributes API
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