简体   繁体   English

Rails数据建模:当两个对象“共享”相同的has_many列表时,如何建立关系?

[英]Rails Data Modelling: How to establish relationships when 2 Objects “Share” the same has_many list?

In a nutshell, when I create a Transaction Record it has two foreign keys . 简而言之,当我创建Transaction记录时,它具有两个外键 for the two users that participate in a Transaction , ie: 对于参与Transaction的两个用户,即:

在此处输入图片说明

Now, What I would like to know with your kind help, is How do I establish the relationship(s) between: User and Transaction Models, so that I can easily retrieve ALL the Transaction s for either of the two User s. 现在,我想在您的帮助下知道如何建立UserTransaction模型之间的关系 ,以便我可以轻松地检索两个User的所有Transaction

Something like: 就像是:

  user_one = User.find(1)
  user_two = User.find(2)

  user_one.transactions # returns all Transactions where user_one.id == 
                        # user_one_id Or user_one.id == user_two_id

  user_two.transactions # returns all Transactions where user_two.id == 
                        # user_one_id Or user_two.id == user_two_id

What's the best way to achieve this? 实现此目标的最佳方法是什么? Is it best to establish foreign keys in the Transaction Model in this case? 在这种情况下,最好在事务模型中建立外键吗? Or is this a problem to be solved via ActiveRecordQuery only? 还是仅通过ActiveRecordQuery解决这个问题?

Thanks in advance. 提前致谢。

If you have two user ids and want to query Transaction on some combination of them, you can use an or clause: 如果您有两个用户ID,并想以它们的某种组合查询Transaction ,则可以使用or子句:

>> Transaction.where(user_one_id: 1).or(Transaction.where(user_two_id: 2))
  Transaction Load (4.3ms)  SELECT  "transactions".* FROM "transactions" WHERE ("transactions"."user_one_id" = $1 OR "transactions"."user_two_id" = $2) LIMIT $3  [["user_one_id", 1], ["user_two_id", 2], ["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<Transaction id: 1, user_one_id: 1, user_two_id: 2, created_at: "2017-09-12 23:25:39", updated_at: "2017-09-12 23:25:39">]>

this is sample code 这是示例代码

class Transaction
  def move
    return ""
  end
end

class User1 < Transaction
  def move
    return 'User1 move: X'
  end
end


class User2 < Transaction
  def move
    return'User2 move: O'
  end
end



transactions = [User1.new, User2.new]
transactions.each {|tran|
  print tran.move
}

使用可以解决此问题的多态关联,我现在在火车上将为您提供代码,或者您可以开始寻找此任务,祝您好运;)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 rails:查询has_many以获取对象列表 - rails: query has_many for a list of objects Rails数据建模:如何为实际上是另一个模型集合的has_many关系建模? - Rails Data Modelling: How can I model a has_many relationship that's actually a collection of another model? 在同一模型之间使用多个has_many关系 - rails multiple has_many relationships between the same models Rails 3.2-如何与现有数据表和外键建立has_many关系 - Rails 3.2 - How to establish a has_many relationship with an existing data table and foreign key 如何通过 Rails 中的关系累积通过“has_many”互连的所有 Active Record 对象的数组 - How to accumulate an array of all Active Record objects that are interconnected via “has_many” through relationships in Rails 何时保存has_many关系中的Active Record对象? - When are Active Record objects in has_many relationships saved? Rails在没有保存时​​有很多关系(可能是ajax相关的) - Rails has_many relationships when not saved (possibly ajax related) 如何在 Rails 上的 Ruby 中使用 ActiveRecord 找到与完全相同的 has_many 关系集的关系? - How to I find relations with the exact same set of has_many relationships using ActiveRecord in Ruby on Rails? 如何在Rails表单中包含has_many关系? - How do I include has_many relationships in a Rails form? Rails数据库关系has_many:through但对于许多关系 - Rails database relationships has_many :through but for many to many relationships
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM