1

I've looked everywhere for a similar error but couldn't find a solution, so in desperation I'm posting here.

My controller has this:

def add_upc
        @upcs = Dvd.add_upc(params[:dogTag], params[:newUpc])
    end

and in the Model we have:

def self.add_upc(dogTag, newUpc)
    existingUpc = Dvd.find(dogTag).dvd_upc2title.find_by_upc(newUpc)
    if existingUpc.nil? 
        createdUpc = Dvd.find(dogTag).dvd_upc2title.create(:upc => newUpc)
        if createdUpc
            upcs = createdUpc
        else
            upcs = 'Error: nothing was created'
        end
    end
end

I've set up a view page to see what's happening and I can see the object being created by createdUpc. I can also confirm that the parameters dogTag and newUpc are being passed correctly. Yet the record is not being added to the table.

Weirdly, this does work if I issue the Dvd.find(dogTag).dvd_upc2title.create(:upc => newUpc) command with the values substituted for the variables from the IRB.

Can't figure out why this is not working. I'm new to Rails so don't know what other error debugging I could use to figure out where the problem lies.

Ideas are welcome.

Thanks.

Edit:

Found the error thanks to RyanWilcox, it was the validation I had set up in the controller for UPC telling me that value already existed (even though UPCs are supposed to be unique. Is there a way to validate on a combination of 2 fields?

2
  • have a look at your log/development.log and try executing Dvd.add_upc with those parameters. there might be some naming issue or the like. so try putting out all the params that you use in your method. Commented Dec 1, 2011 at 18:22
  • @phoet this does give me a clue. The log doesn't show anything when I run Dvd.add_upc from the IRB, but it does return :newUpc which is not what I expected at all. Am I passing the parameters from the controller above correctly? What could the :newUpc symbol mean? Commented Dec 1, 2011 at 19:12

1 Answer 1

1

What I really like doing for situations like this ("why did this fail to save?") is using create! instead of create.

This will throw an exception on error, with the failed validation's text as the message of the exception. It makes problems like this obvious.

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

3 Comments

Ah, fantastic! Didn't know about this. Using create! produces this error: Validation failed: Upc has already been taken. But I'm not sure what this means. Upc (capital) seems to be referring to the model maybe? Those values don't exist in the table, so it's not that.
Found it, it was the validation I had set up in the controller for UPC telling me that value already existed. Is there a way to validate on a combination of 2 fields?
That's a good question, and I don't know the answer off the top of my head. My suggestion is to do some googling or open another SO question. (it's probably a "possible, but non-obvious" answer).

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.