h.slice(:key1,:key2)
h.slice(*[:key1,:key2])
h.symbolize_keys
note
Rails 6 adds insert_all, insert_all!, upsert_all for bulk insertion
insert_all(attributes, returning: nil, unique_by: nil)
This method can be used to insert multiple records with a single SQL INSERT statement. The method simply skips ActiveRecord callbacks or validations. It accepts array of hashes, where every hash determines the attributes for a single record or row.
insert_all!(attributes, returning: nil)
This method works same as that of insert_all but in addition to that it raises ActiveRecord::RecordNotUnique if any record violates a unique index on the table.
upsert_all(attributes, returning: nil, unique_by: nil)
It updates the records if they exist or simply inserts them into database with a single SQL INSERT statement.
NOTE:
user_idinstead of
user
https://stackoverflow.com/questions/6698207/delete-all-vs-destroy-all
delete_all: one SQL delete for all matched results.
NOTE:It seems to be update if use reference e.g.
table.datetime_cells.delete_all
DatetimeCell Update All (0.3ms) UPDATE "datetime_cells" SET "table_id" = $1 WHERE "datetime_cells"."table_id" = $2 [["table_id", nil], ["table_id", 7]]
datetime_cells.where(table:talbe).delete_all # OK
gem
gem simple_calendar
scss
include the default stylesheet for the calendar in your app/assets/stylesheets/application.css file
*= require simple_calendar
highlight start-date in app/assets/stylesheets/simple-calender.scss
.simple-calendar {
.start-date {
background-color: coral
}
}
views
<%= month_calendar start_date_param: :date do |date| %>
<%= date.day %>
<% end %>
Gemfile
gem 'wicked_pdf'
gem 'wkhtmltopdf-binary'
#gem 'wkhtmltopdf-binary-edge' # fork of wkhtmltopdf-binary,不需要
install libxrender1
error while loading shared libraries: libXrender.so.1
sudo apt-get install libxrender1
in config/initializers/mime_types.rb
Mime::Type.register "application/pdf", :pdf
define template in app/views/layouts/pdf.html.erb
<!doctype html>
<html>
<head>
<meta charset='utf-8' />
<%= javascript_include_tag "https://code.jquery.com/jquery-3.3.1.slim.min.js" %>
<%= javascript_include_tag "http://code.jquery.com/ui/1.10.3/jquery-ui.min.js" %>
<%= javascript_include_tag "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" %>
<%= javascript_include_tag "https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" %>
</head>
<body onload='number_pages'>
<div id="header">
</div>
<div id="content">
<%= yield %>
</div>
</body>
</html>
<%= wicked_pdf_stylesheet_link_tag "pdf" -%>
<%= wicked_pdf_javascript_include_tag "number_pages" %>
specify template
config/initlializers/wicked_pdf.rb
layout: 'pdf.html'
Use it in controllers
in app/controller/records_controller.rb
format.pdf do
render pdf: "records",
template: "records/index.html.erb"
# layout: "pdf.html"
end
If you use assets, pre-compile it for production. in config/initializers/assets.rb
. e.g.
Rails.application.config.assets.precompile += ['blueprint/screen.css', 'pdf.css', 'jquery.ui.datepicker.js', 'pdf.js']
Chinese character problem.
<meta charset='utf-8' />
sudo apt-get install fonts-wqy-zenhei