【发布时间】:2016-01-09 13:32:37
【问题描述】:
如何查找 id 与其他表匹配的表值。
我有这个表:
Zombie_users Body_status
| id | name | | id| Zombie_id| body_id | status|
|----|------| --> |---|----------|---------|-------|
| 1 | Joe | | 1 | 1 | 2 | true |
Zobmie_users Tools Body_impacts
| id | name | | id |user_id| name | | id| tool_id| body_id | impact |
|----|------|--> |----|-------|------|--> |---|--------|---------|--------|
| 1 | Joe | | 1 | 1 |hammer| | 1 | 1 | 2 | 10% |
我需要找到所有具有user --> Body_status = false 的user -> tools。
我的意思是如果我们有 Body_impact -> body_id -> 2 和 Body_status -> body_id -> 2 也有 status = true 从列表中排除该工具
类似的:
@Body_status = @zombie.Body_status.where( Body_status: { :status=> false } )
@tools = @zombie.tools.includes(:Body_impacts).where( @Body_status.body_id } )
我知道这不是工作代码,但它完美地解释了所需操作的逻辑。
更新
我的模型:
class ZombieUser < ActiveRecord::Base
has_many :body_statuses
has_many :bodies, through: :body_statuses
has_many :tools
class BodyStatus < ActiveRecord::Base
belongs_to :zombie_users
belongs_to :bodies
class Tool < ActiveRecord::Base
belongs_to :zombie_users
has_many :body_impacts
has_many :bodies, :through => :body_impacts
accepts_nested_attributes_for :body_impacts,
class BodysImpact < ActiveRecord::Base
belongs_to :tools
belongs_to :bodies
【问题讨论】:
-
你能在问题中发布你的模型吗?
-
请把你的协会放在这里。
-
@Pavan ,谢谢,我添加了模型。
标签: ruby-on-rails associations rails-activerecord has-many-through has-and-belongs-to-many