0

Can anyone help me in this issue, Like I have two dimensional array like this

array = [[1, 'go'], [15, 'flat']........] 

And I have table in the database called FeedPost, now in the 2D array the first element in each array means to a column user_id and the second one means to the column feed_type. Now I want to get all the records from that table that fulfills that array elements exactly matches to these exact column. in other word i wanted do something like that,

FeedPost.find_all_by_user_id_and_feed_type(array)

but unfortunately it shows following argument error.

ArgumentError: wrong number of arguments (1 for 2)

So anyone can help me in this issue please?

2 Answers 2

1

I think the best solution would be:

FeedPost.where(user_id: array.map(&:first)).where(feed_type: array.map(&:last))

this will split your array into two arrays, one containing the user_ids and one containing the feed_types, and query the feed posts accordingly using an IN SQL statement.

EDIT: this will bring any all records that have any combination of user_id and feed_type from the array. If you want only records that only satisfy both user_id and feed_type from a single item in the array, this won't work.

Sign up to request clarification or add additional context in comments.

4 Comments

with your syntax, you allow a record { user_id: 1, feed_type: 'flat' } to match. Its not desired
Correct. I just added this as an EDIT.
try your query, you'll get zero result: its an AND chaining
Correct again. I'll remove that.
1

Install the any_of gem, then:

queries = array.map {|sub_array| FeedPost.where({ user_id: sub_array[0], feed_type: sub_array[1] }) }
FeedPost.where.any_of(*queries)

3 Comments

i am getting this error ArgumentError: wrong number of arguments (0 for 1)
I need more info, no idea what happens
i hope you will understand what i am trying to do, its a method in a model def leader_posts array = [] self.supports.each do |s| array << [s.leader_id, s.political_issue_id] end queries = array.map {|sub_array| FeedPost.where({ user_id: sub_array[0], political_issue_id: sub_array[1] })} FeedPost.where.any_of(*queries) end @apneadiving

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.