Here is the sample code to know the usage of array mapping in Ruby.
>> User.find_all_by_office_id(20)
=> [#
If you want to map only the User id's from the above result set. then you should use like this:
>> User.find_all_by_office_id(20).map(&:id)
=> [1032, 3646]
If you want to use these User id's in an another query with "IN" predicate. Then you should use like this:
>> User.find_all_by_office_id(20).map(&:id).compact.join(",")
=> "1032,3646"
the compact will you delete the blank array element from the result set.
then it joins with "," (comma). So finally you will get the string with concatenated id's with comma. You can use this string in the another query with "IN" predicate, like this
>>user_ids = User.find_all_by_office_id(20).map(&:id).compact.join(",")
>>y Website.find(:all, :conditions => "user_id in(#{user_ids})")
you will the websites list for these users.
No comments:
Post a Comment