「Rails4技術者認定ブロンズ試験」対応の模擬試験問題です。株式会社Dive into Codeの野呂浩良が出題します。
問題1~5
問題1
BlogモデルとUserモデルのアソシエーションが以下のように定義してあった場合に、エラーが発生しないものを1つ選びなさい。
class Blog < ActiveRecord::Base has_many :comments end class User < ActiveRecord::Base has_many :blogs end def show @blog = Blog.find(params[:id]) end
- 1. @blog.comment
- 2. @blog.user
- 3. @blog.users
- 4. @blog.comments
- 5. user.@blogs
解答・解説
Blogモデル内に「has_many :comments」というアソシエーションを定義すると、Blogモデルオブジェクトから.commentsと指定をすれば値を取得できます。
1.は、.commentと単数形になっているため、has_manyに定義した:commentsを取得することはできません。
2.と3.は、Blogモデル内にUserモデルへのアソシエーションの記載がないために取得できません。
5.は、モデルオブジェクトに対してインスタンス変数をつなげて記載することはできません。
〔正解:4〕
問題2
以下のモデルとテーブルが存在するときにお互いに正しく紐づけることが可能なアソシエーションを1つ選びなさい。
class Blog < ActiveRecord::Base
end
create_table "blogs", force: :cascade do |t|
t.string "title"
t.text "content"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
class Comment < ActiveRecord::Base
end
create_table "comments", force: :cascade do |t|
t.integer "blog_id"
t.integer "user_id"
t.string "content"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
-
1. Commentモデルに
「belongs_to :hogehoge, class_name: 'blogs', foreign_key: "blog_id"」と定義する -
2. Commentモデルに
「belongs_to :hogehoge, class_name: 'Blog', foreign_key: "blog_id"」と定義する -
3. Blogモデルに
「has_many :fugafuga, class_name: 'Comment', foreign_key: "content"」と定義する -
4. Blogモデルに
「has_many :fugafuga, class_name: 'comment', foreign_key: "blog_id"」と定義する -
5. Blogモデルに
「has_many :fugafuga, class_name: 'Comment', foreign_key: "id"」と定義する
解答・解説
アソシエーションは、class_name:オプションを追記することで、モデルクラスを指定することができます。また、foreign_key:オプションを追記することで、紐づける外部キーを指定することができます。
1.は、モデルクラス名が小文字複数形のため誤っています。
3.は、モデルクラス名は正しいのですが、外部キーが文字列のcontentとなっており、正常に紐付けはできません。
4.は、外部キーは正しく指定できていますが、モデルクラス名が小文字で誤っています。
5.は、モデルクラス名は正しいのですが、外部キーが各テーブルの主キーの「id」となっており、正しく紐づけることができません。
〔正解:2〕
問題3
blogsテーブル内のレコードが0件の場合に実行してもエラーとなるものを1つ選びなさい。
- 1. Blog.all
- 2. Blog.where(id: 1)
- 3. Blog.find(1)
- 4. Blog.find_by(id: 1)
- 5. Blog.find_by_sql(‘SELECT * FROM blogs;’)
解答・解説
3.は、SQL「SELECT "blogs".* FROM "blogs" WHERE "blogs"."id" = $1 LIMIT 1」が実行されます。.find()では、()内に指定したid値で取得できるレコード件数が0件の場合、ActiveRecord::RecordNotFound:が発生します。
1.は、SQL「SELECT "blogs".* FROM "blogs"」が実行されます。エラーにはなりません。
2.は、SQL「SELECT "blogs".* FROM "blogs" WHERE "blogs"."id" = $1」が実行されます。エラーにはなりません。
4.は、SQL「SELECT "blogs".* FROM "blogs" WHERE "blogs"."id" = $1 LIMIT 1」が実行されます。エラーにはなりません。
5.は、SQL「SELECT * FROM blogs;」が実行されます。エラーにはなりません。
〔正解:3〕
問題4
BlogモデルとCommentモデル、それぞれに対応するblogsテーブルとcommentsテーブルが存在し、以下のアソシエーションが設定されている場合に、blogsテーブルとcommentsテーブルが紐づくレコードのみ取得(内部結合)できるものを選びなさい。
class Blog < ActiveRecord::Base
has_many :comments
end
create_table "comments", force: :cascade do |t|
t.integer "blog_id"
t.integer "user_id"
t.string "content"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
class Comment < ActiveRecord::Base
belongs_to :blog
end
create_table "blogs", force: :cascade do |t|
t.string "title"
t.text "content"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
- 1. Blog.joins(:comment)
- 2. Blog.joins(:comments)
- 3. Blog.eager_load(:comments)
- 4. Blog.eager_load(:comment)
- 5. Blog.includes(:comments)
解答・解説
joins()とすることで内部結合ができます。()内には、いずれのメソッドでもアソシエーション名を指定します。
アソシエーションで定義していないものは使うことができません。eager_loadやincludesは外部結合をします。そのため、commentsレコードに紐付かないblogsレコードも取得してしまいます。
〔正解:2〕
問題5
Ruby on Railsアプリケーション内の開発環境のみからGmail経由でメール送信ができるようにしたい。設定として適しているものを1つ選びなさい。
- 1. config/environments/development.rbに以下を記載する
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'gmail.smtp.com',
port: 587,
domain: 'example.com',
user_name: '<ユーザー名>',
password: '<パスワード>',
authentication: 'plain',
enable_starttls_auto: true }
- 2. config/environments/development.rbに以下を記載する
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'example.com',
user_name: '<ユーザー名>',
password: '<パスワード>',
authentication: 'plain',
enable_starttls_auto: true }
- 3. config/environments/production.rbに以下を記載する
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'gmail.smtp.com',
port: 587,
domain: 'example.com',
user_name: '<ユーザー名>',
password: '<パスワード>',
authentication: 'plain',
enable_starttls_auto: true }
- 4. config/environments/production.rbに以下を記載する
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'example.com',
user_name: '<ユーザー名>',
password: '<パスワード>',
authentication: 'plain',
enable_starttls_auto: true }
解答・解説
開発環境の設定は、config/environments/development.rbに記載します。また、address:の書き方は、「'smtp.gmail.com'」が正しいです。
〔正解:2〕

