3. Eager Loading
Associations
- Conditions on Eager Loaded Associations -
conditional “joins” > conditional “includes”
Post.includes(:comments)
.where("comments.visible", true)
SELECT "posts"."id" AS t0_r0, ... "comments"."updated_at" AS t1_r5
FROM "posts"
LEFT OUTER JOIN "comments"
ON "comments"."post_id" = "posts"."id"
WHERE (comments.visible = 1)
ROR Lab.
4. Eager Loading
Associations
- Conditions on Eager Loaded Associations -
INNER JOIN LEFT OUTER JOIN
conditional “joins” > conditional “includes”
Post.includes(:comments)
.where("comments.visible", true)
SELECT "posts"."id" AS t0_r0, ... "comments"."updated_at" AS t1_r5
FROM "posts"
LEFT OUTER JOIN "comments"
ON "comments"."post_id" = "posts"."id"
WHERE (comments.visible = 1)
ROR Lab.
5. Scopes
Passing in arguments
class Post < ActiveRecord::Base
scope :1_week_before,
lambda { |time| where("created_at < ?", time) }
end
class Post < ActiveRecord::Base
def self.1_week_before(time)
where("created_at < ?", time)
end
***What about “as a class method” ?
ROR Lab.
6. Scopes
Passing in arguments
class Post < ActiveRecord::Base
scope :1_week_before,
lambda { |time| where("created_at < ?", time) }
end
le
eab
erclass Post < ActiveRecord::Base
ef
pr def self.1_week_before(time)
where("created_at < ?", time)
end
***What about “as a class method” ?
ROR Lab.
17. HTML 5
Form Helpers
Rails 3 HTML5 Input Types
search_field search
telephone_field tel
url_field url
email_field email
number_field number
range_field range
Agile Web Development with Rails 4th edition
ROR Lab.
74. Uploading Files
params[:picture]
<%= form_tag({:action => :upload}, :multipart => true) do %>
<%= file_field_tag 'picture' %>
<% end %>
<%= form_for @person
, :multipart => true
<%= f.file_field :picture %> do |f| %>
<% end %>
params[:person][:picture]
Since Rails 3.1, it automatically sets the multipart/
form-data with file_field in the form_for
ROR Lab.
75. Uploading Files
params[:picture]
<%= form_tag({:action => :upload}, :multipart => true) do %>
<%= file_field_tag 'picture' %>
<% end %>
<%= form_for @person
<%= f.file_field :picture %> |f| %>
do
<% end %>
params[:person][:picture]
Since Rails 3.1, it automatically sets the multipart/
form-data with file_field in the form_for
ROR Lab.
76. Uploading Files
params[:picture]
<%= form_tag({:action => :upload}, :multipart => true) do %>
<%= file_field_tag 'picture' %>
<% end %>
<%= form_for @person
<%= f.file_field :picture %> |f| %>
do
<% end %>
params[:person][:picture]
Since Rails 3.1, it automatically sets the multipart/
form-data with file_field in the form_for
ROR Lab.
77. What gets
uploaded
def upload
uploaded_io = params[:person][:picture]
File.open(Rails.root.join('public', 'uploads',
uploaded_io.original_filename), 'w') do |file|
file.write(uploaded_io.read)
end
ROR Lab.
78. What gets
uploaded
def upload
uploaded_io = params[:person][:picture]
File.open(Rails.root.join('public', 'uploads',
uploaded_io.original_filename), 'w') do |file|
file.write(uploaded_io.read)
end
an instance of a subclass of IO
• original_filename
• content_type : MIME type
ROR Lab.
97. Form to External
Resources - 1
<%= form_tag 'http://farfar.away/form', :authenticity_token =>
'external_token') do %>
Form contents
<%= form_tag 'http://farfar.away/form', :authenticity_token =>
false) do %>
Form contents
ROR Lab.
98. Form to External
Resources - 1
<%= form_tag 'http://farfar.away/form', :authenticity_token =>
'external_token') do %>
Form contents
payment gateway
<%= form_tag 'http://farfar.away/form', :authenticity_token =>
false) do %>
Form contents
ROR Lab.
99. Form to External
Resources - 1
<%= form_tag 'http://farfar.away/form', :authenticity_token =>
'external_token') do %>
Form contents
<%= form_tag 'http://farfar.away/form', :authenticity_token =>
false) do %>
Form contents
payment gateway
ROR Lab.
100. Form to External
Resources - 2
<%= form_for @invoice, :url =>
external_url, :authenticity_token => 'external_token' do |f|
Form contents
<% end %>
<%= form_for @invoice, :url =>
external_url, :authenticity_token => false do |f|
Form contents
ROR Lab.
101. Form to External
Resources - 2
<%= form_for @invoice, :url =>
form_for
external_url, :authenticity_token => 'external_token' do |f|
Form contents
<% end %>
<%= form_for @invoice, :url =>
form_for
external_url, :authenticity_token => false do |f|
Form contents
ROR Lab.
103. Complex forms
Railscasts by Ryan Bates
• Complex Forms Part 1 - Episode #73
• Complex Forms Part II - Episode #74
• Complex Forms Part III - Episode #75
• Nested Model Form (resivsed) - Episode #196
ROR Lab.