Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个用户模型,一个用户有很多前辈。我想返回所有 Senior_ids 列中包含 id=x 的用户记录。我能想到的最好的方法是 User.where("?IN Senior_ids", x),但这不起作用。有什么建议么?
看来您的设计在这里略有错误。您应该使用 has_and_belongs_to_many 关联(或 has_many :through)来连接用户和老年人。您可以在 nzifnab 评论中的链接下阅读这些内容。然后你可以简单地写:
senior.users
使用当前设计,您可以使用:
User.all.select {|user| user.senior_ids.include? x}
但是它会变得非常缓慢而且非常丑陋。保持序列化的 id 数组通常是一个非常糟糕的主意。