Ruby on rails Rails 3=>;:有多个X,通过=>;Y、 Z
我正试着做类似的事情Ruby on rails Rails 3=>;:有多个X,通过=>;Y、 Z,ruby-on-rails,associations,Ruby On Rails,Associations,我正试着做类似的事情 class Event < ActiveRecord::Base has_many :links, :dependent => :destroy belongs_to :activity belongs_to :facility has_many :infos, :through => :activity has_many :infos, :through => :facility accepts_n
class Event < ActiveRecord::Base
has_many :links, :dependent => :destroy
belongs_to :activity
belongs_to :facility
has_many :infos, :through => :activity
has_many :infos, :through => :facility
accepts_nested_attributes_for :infos
end
class事件:销毁
属于:活动
属于:设施
有许多:信息,:通过=>:活动
有很多:信息,:通过=>:facility
接受\u嵌套的\u属性\u for:infos
结束
但这导致有许多:信息,:通过=>:facility
,而有许多:信息,:通过=>:activity
被忽略
我如何用正确的语法表达这一点
编辑:
如果我实现Vlad的方法,然后使用
@事件=Event.all(:include=>[:facility\u infos,:activity\u infos],:conditions=>[“infos.language\u id=?”,2],:order=>:time)
我得到了语言id=2的:activity\u infos
,但不幸的是,我得到了所有语言的:facility\u infos
!
如何解决这个问题
编辑2:
以下是您请求的输出:
选择“事件”。“id”作为t0_r0,“事件”。“时间”作为t0_r1,“事件”。“每天”作为t0_r2,“事件”。“活动id”作为t0_r3,“事件”。“创建”作为t0_r4,“事件”。“更新”作为t0_r5,“事件”。“设施id”作为t0_r6,“事件”。“家”作为t0_r7,“信息”。“id”作为t1_r0,“信息”。“标题”作为t1_r1,“信息”。“描述”作为t1_r2“信息位置”。“作为t1_r3,“信息系统”,“语言id”作为t1_r4,“信息系统”,“创建于”作为t1_r5,“信息系统”,“更新于”作为t1_r6,“信息系统”,“活动id”作为t1_r7,“信息系统”,“设施id”作为t1_r8,“信息系统”,“服务id”作为t1_r9,“活动信息系统事件”,“id”作为t2_r0,“活动信息系统事件”,“标题”作为t2_r1,“活动信息系统事件”,“描述”作为t2_r2,“活动信息事件”。“位置”为t2_r3,“活动信息事件”。“语言id”为t2_r4,“活动信息事件”。“创建于”为t2_r5,“活动信息事件”。“更新于”为t2_r6,“活动信息事件”。“活动id”为t2_r7,“活动信息事件”。“设施id”为t2_r8,“活动信息事件”。“服务id”为t2_r9“活动”上的“左外连接”设施。“设施id”=“设施”。“信息”上的“id”左外连接“信息”。“设施id”=“设施”。“活动”上的“id”左外连接“活动”。“活动id”=“活动”。“活动id”=“活动”。“活动信息”事件上的“id”左外连接“信息”。“活动id”=“活动”。“id”在哪里(infos.language_id=2)按时间排序
has_many :activity_infos, :through => :activity, :source => :infos, :class_name => "Info"
has_many :facility_infos, :thorough => :facility, :source => :infos, :class_name => "Info"
def infos
activity_infos + facility_infos
end
更新:
请显示以下输出:
Event.includes([:facility_infos, :activity_infos]).where(['infos.language_id = ?', 2]).to_sql
更新2:
看起来像
Event.includes([:facility_infos, :activity_infos]).where(['infos.language_id = ? AND activity_infos_events.language_id = ?', 2, 2])
应该返回您期望的内容?更新了我的帖子。这应该会返回sql查询,以便我们可以检查发生了什么事Hi Vlad,非常感谢您的回答。不幸的是,我仍然有一些问题…请您查看我的编辑?谢谢!太好了,就是这样!哦,谢谢“.to_sql”技巧,这将非常有用!