質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Q&A

解決済

1回答

1123閲覧

paperclipのschema.rbメソッドの内容で分からない部分があります。

setsu_tenhou

総合スコア33

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

0グッド

0クリップ

投稿2017/12/23 05:15

schema.rb内に以下の様なコードがあるのですが、add_attachmentメソッドの中央にカラムのオプションを変数column_optionsに格納しているような記載が見られます。
options.mergeとありますが、optionsインスタンスも見当たりません。
ここで使われているoptionsインスタンスとはなんなのでしょうか?

###詳細
paperclip (5.0.0)
Rails 5.1.4

ruby

1require 'active_support/deprecation' 2 3module Paperclip 4 # Provides helper methods that can be used in migrations. 5 module Schema 6 COLUMNS = {:file_name => :string, 7 :content_type => :string, 8 :file_size => :integer, 9 :updated_at => :datetime} 10 11 def self.included(base) 12 ActiveRecord::ConnectionAdapters::Table.send :include, TableDefinition 13 ActiveRecord::ConnectionAdapters::TableDefinition.send :include, TableDefinition 14 ActiveRecord::ConnectionAdapters::AbstractAdapter.send :include, Statements 15 ActiveRecord::Migration::CommandRecorder.send :include, CommandRecorder 16 end 17 18 module Statements 19 def add_attachment(table_name, *attachment_names) 20 raise ArgumentError, "Please specify attachment name in your add_attachment call in your migration." if attachment_names.empty? 21 22 options = attachment_names.extract_options! 23 24 attachment_names.each do |attachment_name| 25 COLUMNS.each_pair do |column_name, column_type| 26 column_options = options.merge(options[column_name.to_sym] || {}) 27 add_column(table_name, "#{attachment_name}_#{column_name}", column_type, column_options) 28 end 29 end 30 end 31 32 def remove_attachment(table_name, *attachment_names) 33 raise ArgumentError, "Please specify attachment name in your remove_attachment call in your migration." if attachment_names.empty? 34 35 attachment_names.each do |attachment_name| 36 COLUMNS.keys.each do |column_name| 37 remove_column(table_name, "#{attachment_name}_#{column_name}") 38 end 39 end 40 end 41 42 def drop_attached_file(*args) 43 ActiveSupport::Deprecation.warn "Method `drop_attached_file` in the migration has been deprecated and will be replaced by `remove_attachment`." 44 remove_attachment(*args) 45 end 46 end 47 48 module TableDefinition 49 def attachment(*attachment_names) 50 options = attachment_names.extract_options! 51 attachment_names.each do |attachment_name| 52 COLUMNS.each_pair do |column_name, column_type| 53 column_options = options.merge(options[column_name.to_sym] || {}) 54 column("#{attachment_name}_#{column_name}", column_type, column_options) 55 end 56 end 57 end 58 59 def has_attached_file(*attachment_names) 60 ActiveSupport::Deprecation.warn "Method `t.has_attached_file` in the migration has been deprecated and will be replaced by `t.attachment`." 61 attachment(*attachment_names) 62 end 63 end 64 65 module CommandRecorder 66 def add_attachment(*args) 67 record(:add_attachment, args) 68 end 69 70 private 71 72 def invert_add_attachment(args) 73 [:remove_attachment, args] 74 end 75 end 76 end 77end 78

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

optionsは、少し上の行で、options = attachment_names.extract_options!としているローカル変数です。

Rubyの場合、ローカル変数の参照とselfのメソッドの引数なしの呼び出しは同じ形となります。

投稿2017/12/23 05:19

maisumakun

総合スコア145184

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

setsu_tenhou

2017/12/23 07:24

回答どうもありがとうございます。 つい見逃していました。。。 ローカル変数なのですね、てっきりインスタンスだと思っていました。。。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問