0

Good Day All!

Edited for better understanding.

First model is Inventory and in this model I have Product_Type, Product_Name and User_ID.

Second model I have Users which consist of First_Name, Last_Name and Pin_Number.

On my Inventories page I have a form for checking out said Product_Type and Product_Name, also a place for a user to put their Pin_Number in. On submit, it will check the Pin_Number they have typed in and validate it in the Users model and if the Pin_Number is correct it will create an entry with said Product_Type, Product_Name and User_ID (which is pulled from Pin_Number that was submitted.)

I am just trying to figure out how to validate that Pin_Number they submitted.

Thats why I thought I had to do some kind of validation and an if statement based on that validation. Not sure how to go about that.

I hope this clears up any confusion.

Allen
  • 79
  • 1
  • 1
  • 7

2 Answers2

1

I am just trying to figure out how to validate that Pin_Number they submitted.

What constitutes a valid pin_number? Just that it allows you to successfully look up a User? What if a user enters another user's pin_number? Is that considered 'valid'? Something to think about...

It would be helpful if you would add to your question what your params look like upon form submission. But, we can do some guess work.

So, let's assume that params looks something like:

{..., "inventory"=>{"product_type"=>"foo", "product_name"=>"Bar"}, "pin_number"=>5, ...}

In your controller, you'll probably do something like:

if @user = User.find_by(pin_number: params[:pin_number])
  @inventory = Inventory.new(inventory_params)
  @inventory.user = @user
  if @inventory.valid?
    @inventory.save
    # perhaps do some other stuff...
  else 
    # handle the case where the `@inventory` is not valid
  end
else
  # handle the case where the `@user` was not found
end

This assumes you have something like:

private 

  def inventory_params
    params.require(:inventory).permit(:product_type, :product_name)
  end

In your Inventory model, you probably want to do something like (I apologize, I'm not on Rails 5 yet, so some of the syntax may be incorrect):

class Inventory < ActiveRecord::Base 

  validates :user_id,
            :product_type,
            :product_name,
            presence: true

  belongs_to :user

end

You probably also want to consider adding an index on User.pin_number if you're going to be doing a lot of finding by it.

jvillian
  • 18,961
  • 5
  • 26
  • 41
  • See Edited Question. – Allen Aug 09 '17 at 22:05
  • This was what I was looking for, I couldn't figure out how to do it. But this example was what I was thinking. Thank you! – Allen Aug 09 '17 at 22:43
  • Glad to help. Also, it was really great that you clarified your question - super helpful! – jvillian Aug 09 '17 at 22:44
  • So I am getting an error. "No template found for InventoriesController#create, rendering head :no_content" not sure what that is about but also, i added id to@inventory.user = @user.id, that should give me the user id instead of pin number right? The reason I ask is because I want to grab user_id based on the pin number. – Allen Aug 10 '17 at 19:30
  • On the `No template` error, you should post a new question. On the second part, you don't need to do `@inventory.user = @user.id` because Rails knows how to use the `id` field already based on your `belongs_to :user` statement. – jvillian Aug 10 '17 at 19:46
  • Oh ok, Thank again jvillian! I will post new question for that error. – Allen Aug 10 '17 at 20:00
  • jvillian, i am having an issue with this. it seems it only presents the input of the pin when i want to use the pin to lookup the user's data and then add the user's id for user_id. i can do this in rails console but not in the app. any suggestions? – Allen Aug 22 '17 at 23:14
  • example of output it shows: "inventory"=>{"eq_type_id"=>"1", "equipment_id"=>"1", "chauffeur_id"=>"1", "user_id"=>"1985"} but i want user_id to be the users id not the pin number. – Allen Aug 22 '17 at 23:40
  • Bummer! Unfortunately, I can't debug your code in the comments section (too hard, and that's not what comments are for). If you like, post a new question and mention me in the comments. I'll try to take a look at it. But, I'll need more to go on that what you have above. – jvillian Aug 23 '17 at 14:58
  • i appreciate the help. I will post a new one and give everything i can about what i currently have and trying. – Allen Aug 23 '17 at 15:09
0

Not sure if I got the question right, but sounds like a custom validator to me.

You can learn more about custom validators in the official Rails documentation under "Custom Validators"

Also, consider moving the class for the custom validator you'll build to a concern, which is a great way to make it reusable. You can find more information on this StackOverflow question and this nice tutorial.

Fotis
  • 1,160
  • 13
  • 30