Ruby on rails Rails 3.2和按顺序处理外键

Ruby on rails Rails 3.2和按顺序处理外键,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,因此,我有一个表(为了简单起见,我们称它为a),其中有4个整数列(slot1,slot2,slot3,slot4)。这4个对应于不同表中的一个ID(B,与示例保持一致) 我需要从B中的相应行中提取数据,但这里遇到了一个问题。我正在尝试使用where()方法获取4个插槽,如下所示: @a_item = A.where("publish_at <= ?", DateTime.now).last @slots = B.where(:id => [@a_item.slot1, @a_item

因此,我有一个表(为了简单起见,我们称它为
a
),其中有4个整数列(
slot1
slot2
slot3
slot4
)。这4个对应于不同表中的一个ID(
B
,与示例保持一致)

我需要从
B
中的相应行中提取数据,但这里遇到了一个问题。我正在尝试使用
where()
方法获取4个插槽,如下所示:

@a_item = A.where("publish_at <= ?", DateTime.now).last
@slots = B.where(:id => [@a_item.slot1, @a_item.slot2, @a_item.slot3, @a_item.slot4]) if @a_item != nil
@a_item=a.where(“发布在

我将允许您添加其余的恢复力检查和故障保护,以防在您期望的位置没有返回任何内容。

我不确定您想要返回什么。您想要列出B对象的顺序,其中id位于@a\U项目插槽数组中?您能澄清您想要响应的顺序吗?
@a_item = A.order(:published_at).last
ids = [@a_item.slot1, @a_item.slot2, @a_item.slot3, @a_item.slot4] unless @a_item.nil?
@slots = B.find(ids) unless ids.nil?
results = ids.map { |id| @slots.detect { |slot| slot.id == id } }