1

I have an array of arrays like [["2","3"], ["3","1"], ["6", "1"]]. The first element of each sub-array is the user ID and the second one is the number of seats the user reserved for an event. I want to allow each user to view his reservations by finding his ID in the array. Suppose that I have two models: User and Event. In the User controller, I want to use a scope like @mybooking = Event.mybooking(current_user.id) and the problem is how to write the proper scope in the Event model? And, if the user is found, I want to use its second element, too.

I tried different solutions but didn't work! Let me know if you think it's not possible using the scope and if you have another kind of solution.

Edit: As I'm still waiting for a solution that works, I should mention that I'm looking for something like this:

scope :mybookings, ->(id){where("reservations.to_enum.map{|n,m| n} ?", id)}

or

scope :mybookings, ->(id) { where("reservations.map(&:first) ?", id) }

These two don't work because of the error I get related to "...." part. And, below solution isn't true because I'm calling the Event's scope from User controller and it's not possible to use reservations in that controller because this variable is for the Event controller.

2
  • scope :mybooking, ->(current_user_id) { where user_id: current_user_id } Commented Nov 15, 2016 at 16:17
  • @mudasobwa It's not true! Check the question again! Commented Nov 16, 2016 at 15:22

1 Answer 1

2
class Event
  scope :mybooking, ->(user_ids) { where(user_id: user_ids) }
end

Now it is possible to do in controller:

reservations = [["2","3"], ["3","1"], ["6", "1"]]
Event.mybooking(reservations.map(&:first))
Sign up to request clarification or add additional context in comments.

7 Comments

I don't think that it works! The array is something like this: reservations = [["2","3"], ["3","1"], ["6", "1"]], how can it find the user ids in the first sub-element of the array? (2, 3, and 6 are user ids)
@AboozarRajabi see the edit. Just map the array by first element (id) and pass the resulting array into the scope. Let me know whether it works for you
Thanks but I don't understand how you pass the user_id to the scope?! Another problem is that I think you'll get error because you're using the reservations in the other controller! I think you don't have access to it.
@AboozarRajabi 1. You pass user_ids array, because reservations.map(&:first) results in ["2", "3", "6"]. 2. I am not using reservations in any controller, it is you who use it and whose task to pass the reservations array correctly to the place, where you'll extract the user ids and pass to the scope :)
Using reservations.map(...) results in this error: NameError (undefined local variable or method `passengers' for #<UsersController . It's because reservations that is for the Event controller is used in the User controller. Do you know how should I solve this?
|

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.