Skip to content

Instantly share code, notes, and snippets.

@pinzolo
Last active August 29, 2015 14:05
Show Gist options
  • Save pinzolo/3e8d87e3ffa581ef3b03 to your computer and use it in GitHub Desktop.
Save pinzolo/3e8d87e3ffa581ef3b03 to your computer and use it in GitHub Desktop.
アソシエーションで定義された属性の名前を取得する
user = User.first
# association で定義された属性の名前を取得
user.reflections.keys
# belongs_to で定義された属性の名前を取得
user.reflections.select { |_, ref| ref.macro == :belongs_to }.keys
# through 経由で定義された属性の名前を取得
user.reflections.select { |_, ref| ref.is_a?(ActiveRecord::Reflection::ThroughReflection) }.keys
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment