1

I have an sequel model. I am trying to do validation. I have validation

validates_unique :category

which checks if the same name of category is not taken. It is okay, but I added subcategories (tree). So every record has parent_id if it is subcategory.

I would like to check for duplicates on the same level. I mean that you cannot have duplicate name in top level or in same category, but you can have

buildings
gallery > buildings
names

I was trying to use the examples from documentation, like this

validates_unique(:category, [:category, :parent_id])

to check for combination same parent_id (is number or null) and name. But this makes syntax error

syntax error, unexpected ',', expecting ')'
alidates_unique (:category, [:parent_id, :category])

I tried different combination with parenthesis or without, but with no succes.

validates_unique([:category, :parent_id])

Do you have any clue please?

Mlok
  • 155
  • 9
  • **Syntax** error?! There's nothing syntactically wrong with `validates_unique(:category, [:category, :parent_id])`! – Tom Lord Nov 29 '17 at 11:37
  • Can you please double check exactly what you wrote? I suspect you didn't save the file after deleting a trailing comma, or something. – Tom Lord Nov 29 '17 at 11:39
  • I will make it again. I made it more than once times, I am deploying over git, give me a minute – Mlok Nov 29 '17 at 11:42
  • /usr/local/lib/ruby/gems/2.4.0/gems/padrino-core-0.14.1.1/lib/padrino-core/reloader.rb:96:in `require' /usr/local/lib/ruby/gems/2.4.0/gems/padrino-core-0.14.1.1/lib/padrino-core/reloader.rb:96:in `require': /www/osadababa.cz/osadababa/app/models/category.rb:37: syntax error, unexpected ',', expecting ')' (SyntaxError) alidates_unique (:category, [:parent_id, :category]) ^ from /usr/local/lib/ruby/gems/2.4.0/gems/padrino-core-0.14.1.1/lib/padrino-core/reloader.rb:96:in `block in safe_load' – Mlok Nov 29 '17 at 11:44
  • Why does it say `alidates` instead of `validates`, and also with an extra whitespace? Is that in a different file? – Tom Lord Nov 29 '17 at 11:51
  • No it is in the middle of model. I don't know where is "v" lost, it looks like bad output buffer in terminal, I checked for whitespaces and that is not problem. – Mlok Nov 29 '17 at 11:55
  • when I change it just to validates :category or validates [:category] it works like a charm, but not with the problem I need to solve – Mlok Nov 29 '17 at 11:56
  • I find this a bit odd how you ned to *deploy* the application in order to debug it!! Don't you have it running locally?! – Tom Lord Nov 29 '17 at 11:59
  • I'm still at a loss why this is happening, to be honest. There's clearly no **syntax** error in that line of code, so something strange must be going on in your deployment process. The bizarre error message, that doesn't even show the correct code, is a red flag to me. – Tom Lord Nov 29 '17 at 12:00
  • It is not deployment on production, it is on test, I dont have environment on my macbook, I deploy it on test server with complete environment. If it is finished, it goes to deb package and to test server, then to production, don't worry. – Mlok Nov 29 '17 at 12:01
  • It is simply git pull on debian machine directory, started with rackup, nothing strange on it, I am using it for all the projects. I copied my directory with rsync a tried on another machine, same error. I am lost too, when I use simple (all category) validation it works, it hates this advanced syntax – Mlok Nov 29 '17 at 12:03
  • What is "advanced syntax"? Anything involving a pair of brackets?! Maybe you have some strange character encoding issue on the machine. – Tom Lord Nov 29 '17 at 12:08
  • I thought not just validate simple value but some combination of more parameters with parentheses – Mlok Nov 29 '17 at 12:28
  • I just played around in the console, and I *can* reproduce your error if you put a space between the method name and the opening bracket. In your code sample above, you didn't do that; but the error message does show a space. So once again, I'll ask: Can you please check that your code is the **same** as what you wrote above?? No extra spaces?! – Tom Lord Nov 29 '17 at 13:50
  • `validates_unique(:category, [:category, :parent_id])` should work fine, and `validates_unique :category, [:category, :parent_id]` should also work fine, but `validates_unique (:category, [:category, :parent_id])` does not. – Tom Lord Nov 29 '17 at 13:52
  • I am stupid, I had a space as You showed me, which I did not see, in repetitive looking, yes it is working now. Thanks a lot and sorry for my blindness. – Mlok Nov 30 '17 at 08:39

1 Answers1

1

In method call cannot be a space between method and parameters. Like this

validates_unique (:category, [:category, :parent_id]) 

It has to be without space

validates_unique(:category, [:category, :parent_id]) 

I did not see it, in the repetitive readings. Due to tunnel vision and focusing on different parts. I also enlarge my font in my computer. (I have medical issue, loosing sight on right eye).

Mlok
  • 155
  • 9