0
@video = Video.find(params[:id])

@lengths = @video.each do |i|
  @length = i.length * 60
end

Firstly, I presumed @video would have an each, but instead got this error:

undefined method `each' for #<Video:0x4738428>

Secondly, is there any Ruby one-liner magic that could substitute for the last 3 lines?

2 Answers 2

4

@video is not an array in this case, it is an object because you asked the Video model to return only one video, the one with the id given in the params array.

In case you want to retrieve all videos from the database, do this:

@video = Video.all

Now @video will be an array of video objects.

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

1 Comment

Also, if you just want a single Video object but as an array, just do this: @videos = [Video.find(params[:id])]
1

Pizzicato answered your question just fine, but here's my two cents:

If you would like to know the class of an object, you can do:

p Video.find(params[:id]).class

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.