Ruby-for-Beginner | scripting language designed by Yukihiro Matsumoto
kandi X-RAY | Ruby-for-Beginner Summary
kandi X-RAY | Ruby-for-Beginner Summary
Ruby is a scripting language designed by Yukihiro Matsumoto, also known as Matz. It runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of Ruby-for-Beginner
Ruby-for-Beginner Key Features
Ruby-for-Beginner Examples and Code Snippets
Community Discussions
Trending Discussions on Ruby-for-Beginner
QUESTION
I am following this post and trying to add a validate method before object creation. I keep getting undefined method 'video' for #
I am reading this article here http://ruby-for-beginners.rubymonstas.org/ getting to know how ruby works but cannot figure out why I get this error.
http://api.rubyonrails.org/v5.1/classes/ActiveModel/Validations/ClassMethods.html#method-i-validate
If I call new on a ruby object and save it with a validation what do I need to do to the video variable to not get undefined method
?
ANSWER
Answered 2017-Jul-19 at 09:04as video
method actually doesn't exists on your model, and based on post you're following it's actually a reference you should just simple replace video
with self
. Something like this: if self.reload.count >= 9 errors.add(:base, "Exceeded video limit of 9 videos") end Let me know if this works for you ! Regards
New Answer :
On post you're following, they are checking videos
as association and then checking if its count bigger then 9.
How it works is actually is that the main object which is referenced to the videos
is not stored to DB but it has videos
association so you can check length/count of that.
In your case, if you're trying to do that on your main object ( Video
) you will get an error that it couldn't be find as it's not persisted but just instantiated as well as your validation method is called before create
.
This way you're trying to achieve, it's logical only for validating creating some referenced model, which can't be stored has more then 9 videos.
Hope it's clear enough ?
Regards
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install Ruby-for-Beginner
On a UNIX-like operating system, using your system’s package manager is easiest. However, the packaged Ruby version may not be the newest one. There is also an installer for Windows. Managers help you to switch between multiple Ruby versions on your system. Installers can be used to install a specific or multiple Ruby versions. Please refer ruby-lang.org for more information.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page