通过不同的名称将相同的模型关联两次(Associates the same model twice through different name)

编程入门 行业动态 更新时间:2024-10-24 04:47:38
通过不同的名称将相同的模型关联两次(Associates the same model twice through different name)

我正在尝试实现丢失和找到的数据库。 我有两个模型, User和Item 。 用户可以丢失项目并找到项目。 并且项目可以包含找到它的用户和丢失它的用户。 我希望能够通过不同的名称引用相同的模型,例如

user.found_items, user.lost_items, item.founder, item.losser

现在我能做到:

user.founds , user.losts和user.items将返回user.items的items

class User < ActiveRecord::Base has_many :founds has_many :items, through: :founds has_many :losts has_many :items, through: :losts end class Lost < ActiveRecord::Base belongs_to :user belongs_to :item end class Found < ActiveRecord::Base belongs_to :user belongs_to :item end class Item < ActiveRecord::Base has_one :found has_one :user, through: :found has_one :lost has_one :user, through: :lost end

I am trying to implement a lost and found database. I have two model, User and Item. A user can lost an an item and found an item. And a item can have a the user who found it and the user who lost it. I want to be able to reference the the same model through different name, e.g.

user.found_items, user.lost_items, item.founder, item.losser

right now I am able to do:

user.founds, user.losts and user.items will return the items from losts

class User < ActiveRecord::Base has_many :founds has_many :items, through: :founds has_many :losts has_many :items, through: :losts end class Lost < ActiveRecord::Base belongs_to :user belongs_to :item end class Found < ActiveRecord::Base belongs_to :user belongs_to :item end class Item < ActiveRecord::Base has_one :found has_one :user, through: :found has_one :lost has_one :user, through: :lost end

最满意答案

我会做一些非常相似的事情,只是为了清晰起见重命名,并为你想要的功能添加一些方法。

class User < ActiveRecord::Base has_many :found_items has_many :items, through: :found_item has_many :lost_items has_many :items, through: :lost_item def items_found self.found_items.map {|i| i.item.name } end def items_lost self.lost_items.map {|i| i.item.name } end end class LostItem < ActiveRecord::Base belongs_to :user belongs_to :item end class FoundItem < ActiveRecord::Base belongs_to :user belongs_to :item end class Item < ActiveRecord::Base has_one :found_item has_one :user, through: :found_item has_one :lost_item has_one :user, through: :lost_item def finder self.found_item.user.name end def loser self.lost_item.user.name end end

I would do something pretty similar just rename them for clarity and add some methods for the functions you wanted.

class User < ActiveRecord::Base has_many :found_items has_many :items, through: :found_item has_many :lost_items has_many :items, through: :lost_item def items_found self.found_items.map {|i| i.item.name } end def items_lost self.lost_items.map {|i| i.item.name } end end class LostItem < ActiveRecord::Base belongs_to :user belongs_to :item end class FoundItem < ActiveRecord::Base belongs_to :user belongs_to :item end class Item < ActiveRecord::Base has_one :found_item has_one :user, through: :found_item has_one :lost_item has_one :user, through: :lost_item def finder self.found_item.user.name end def loser self.lost_item.user.name end end

更多推荐

user,Item,User,item,电脑培训,计算机培训,IT培训"/> <meta name="descripti

本文发布于:2023-08-07 06:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1462294.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两次   模型   名称   model   Associates

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!