这是我的问题。我可以使用单个表中的数据填充列表。但我需要读取一个表,并根据这些值从另一个表中获取数据。
有4个表:
帐户调用新的交易方法。 "从"是这里给出的新方法的参数。我能够使用此帐号找到用户ID:
@account=Account.find_by(:acc_no => params[:from])
@transaction.userid = @account.user_id
当我在视图中打印它时,这是有效的。 现在,我需要一个#34;到#34;的下拉列表,该列表应该填充该用户拥有的朋友的所有帐号。为此,我首先发现了这个用户的朋友:
@list1=Friend.where(:user_id => @transaction.userid)
为了测试这个,我只是以这种方式在我的视图中使用了这个列表:
<%= f.select :to, @list1.friendid %>
这也有效。现在,使用list1,我需要生成另一个列表,其中包含list1中存在的id的所有acc_no / accounts
这是我没有输出的地方。我试过这个:
@list2 = @list1.map{|i| Account.where(:user_id => i.friend_id)}
这对我不起作用。基本上我需要执行此查询:
select * from accounts where user_id in (select friend_id from friends where user_id = (select user_id from account where acc_no = params[:from]))
我得到了2个嵌套查询,但无法获得等效的外部查询
答案 0 :(得分:0)
您可以尝试传入ID数组:
list1_ids = @list1.pluck(:id)
@list2 = Account.where(user_id: list1_ids)