Last active
August 29, 2015 14:05
-
-
Save pinzolo/3e8d87e3ffa581ef3b03 to your computer and use it in GitHub Desktop.
アソシエーションで定義された属性の名前を取得する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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