0

I am following this post and trying to add a validate method before object creation. I keep getting undefined method 'video' for #<Video:0xbe430a0> 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?

class Video < ApplicationRecord

  validate :video_count_within_limit, on: :create

  def video_count_within_limit
    if video(:reload).count >= 9
      errors.add(:base, "Exceeded video limit of 9 videos")
    end
  end
Lee Eather
  • 345
  • 3
  • 16
  • Try replacing `video` with `self` – Pavan Jul 19 '17 at 08:22
  • If what you're attempting to do is replicate the same structure from the post you're following, then your Video model should also have `belongs_to :some_model` and then you'd check for the number of videos on that particular models with `self.some_model.video(:reload).count` (although the `self` part is not necessary). The way you're doing it now doesn't make much sense. – Matija Munjaković Jul 19 '17 at 08:28
  • I understand that, I was just trying to do it in one model. Is there another way using just the Video model? Thankyou pavan I did try that before with no luck. – Lee Eather Jul 19 '17 at 08:31

1 Answers1

1

as 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

zauzaj
  • 1,206
  • 12
  • 20
  • Hi. It is giving me error `ActiveRecord::RecordNotFound in VideosController#create Couldn't find Video without an ID` I actually am not sure what that means, I have two videos in database with id: 1 and id: 2? – Lee Eather Jul 19 '17 at 08:47
  • But you're using it in #create action so I suppose your video is still not persisted. Seems you're on wrong way, as post you're following actually check videos as references and that's true. You're trying to check `count` on your instantiated object which is wrong ! – zauzaj Jul 19 '17 at 08:55
  • I dont understand the difference if it is to a user or to a specific model, shouldnt the count count the amount of records in the db? So before create validate runs and returns the errors? – Lee Eather Jul 19 '17 at 09:02
  • Maybe you can just tell what exactly do you want to achieve, it would be much easier to help you ;) – zauzaj Jul 19 '17 at 09:13
  • Yes completely clear. I just wanted to check the count of the videos on the video model before create so that people can not add any more than 9 videos? – Lee Eather Jul 19 '17 at 09:23
  • To be clear this is a test app, my 'real' app has a association, but was doing some testing on one model.. – Lee Eather Jul 19 '17 at 09:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/149571/discussion-between-zauzaj-and-lee-eather). – zauzaj Jul 19 '17 at 09:31