rails 中链方法的别名
alias for chain methods in rails
我有一个用户自加入协会。
这是我目前的数据
Click to view data table
我的用户模型
User.rb
class User < ApplicationRecord
belongs_to :mother, class_name: "User", optional: true
belongs_to :father, class_name: "User", optional: true
end
我可以在 rails 控制台中执行以下操作。
irb(main):001:0> user = User.find(1)
(0.4ms) SELECT sqlite_version(*)
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
=> #<User id: 1, name: "rj", mother_id: 3, father_id: 2>
irb(main):002:0> user.father
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]]
=> #<User id: 2, name: "father", mother_id: 4, father_id: 5>
irb(main):003:0> user.father.father
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 5], ["LIMIT", 1]]
=> #<User id: 5, name: "grandfather", mother_id: nil, father_id: nil>
简而言之,如果user.father.father
或user.father.mother
,我可以得到rj的父亲的父亲(祖父)和rj的父亲的母亲(祖母)的对象
有没有办法让我创建一个别名,例如 user.grandfather
和 user.grandmother
来获得相同的结果?
在User.rb模型中,添加以下方法
def paternal_grandfather
father&.father
end
def paternal_grandmother
father&.mother
end
def maternal_grandfather
mother&.father
end
def maternal_grandmother
mother&.mother
end
或者,在组合形式中,我们可以使用方法作为
def grandfather
father&.father || mother&.father
end
def grandmother
father&.mother || mother&.mother
end
用作user.grandfather
和user.grandmother
class User < ApplicationRecord
belongs_to :mother, class_name: "User", optional: true
belongs_to :father, class_name: "User", optional: true
has_one :grandfather, through: :father, source: :father
has_one :grandmother, through: :father, source: :mother
end
我有一个用户自加入协会。 这是我目前的数据
Click to view data table
我的用户模型
User.rb
class User < ApplicationRecord
belongs_to :mother, class_name: "User", optional: true
belongs_to :father, class_name: "User", optional: true
end
我可以在 rails 控制台中执行以下操作。
irb(main):001:0> user = User.find(1)
(0.4ms) SELECT sqlite_version(*)
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
=> #<User id: 1, name: "rj", mother_id: 3, father_id: 2>
irb(main):002:0> user.father
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]]
=> #<User id: 2, name: "father", mother_id: 4, father_id: 5>
irb(main):003:0> user.father.father
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 5], ["LIMIT", 1]]
=> #<User id: 5, name: "grandfather", mother_id: nil, father_id: nil>
简而言之,如果user.father.father
或user.father.mother
有没有办法让我创建一个别名,例如 user.grandfather
和 user.grandmother
来获得相同的结果?
在User.rb模型中,添加以下方法
def paternal_grandfather
father&.father
end
def paternal_grandmother
father&.mother
end
def maternal_grandfather
mother&.father
end
def maternal_grandmother
mother&.mother
end
或者,在组合形式中,我们可以使用方法作为
def grandfather
father&.father || mother&.father
end
def grandmother
father&.mother || mother&.mother
end
用作user.grandfather
和user.grandmother
class User < ApplicationRecord
belongs_to :mother, class_name: "User", optional: true
belongs_to :father, class_name: "User", optional: true
has_one :grandfather, through: :father, source: :father
has_one :grandmother, through: :father, source: :mother
end