0
@product

returns a single record. By relationship this product belongs to a slot

@slot = Slot.where(['id = ?', @product.slot_id]).first

what needs to be accessed is the position x in the array of all @slots = Slot.order('id asc').all so that I can identify or iterate over the following n slots as per ruby array Class:

arr[x, n]
3
  • I am not understanding the question. What is it you are trying to achieve? Commented Dec 30, 2014 at 15:11
  • @ slots will be an array of index positions 0 to last. @ product.slot_id has an index position and I'd like to extract that. Commented Dec 30, 2014 at 16:29
  • Not quite clearly understanding your question, but are you trying to access @product.slot.position (which seems to be x here) from @slots array ? Commented Dec 30, 2014 at 17:13

2 Answers 2

1

I am not sure I understand the question there are many methods of accessing the index for an Array e.g.

 alphabet = ('a'..'z').to_a
 alphabet[0]
 #=> "a"
 alphabet.values_at(2,3,12)
 #=> ["c","d","m"]
 alphabet.index('r')
 #=> 17
 alphabet.fetch(15)
 #=> "p"

There are many more such as #at, #find_index, even #rindex which will look for the last occurance. If you need to iterate index's you can use each_index or each_with_index. Since your question does not truely explain the scenario all I can do is explain how to deal with Array indices. For a more pertinent answer please update your question to show both data and expected results.

Here is what I can gather from your question

@product = Product.find(some_id)
@slot = @product.slot
@slots = Slot.where("id > ?", @slot.id) #return all slots after the @product.slot
Sign up to request clarification or add additional context in comments.

2 Comments

`index('r') is what I was looking for (and not evidenced in link published in the question.
@Jerome find_index and index (alias) are both documented in the link you posted
0

If want to get the Slot for a given Product you can just do this:

slot = @product.slot

assuming you have your relationships well defined.

Comments

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.