Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional validations #42

Closed
laserlemon opened this issue Feb 8, 2010 · 6 comments
Closed

Optional validations #42

laserlemon opened this issue Feb 8, 2010 · 6 comments

Comments

@laserlemon
Copy link
Contributor

I'm using the latest friendly_id (2.3.0) on Rails 2.3.5. I have an Order model that has a "number" column. The number is populated immediately after creation because the value depends upon knowing the ID of the order. I want to use friendly_id on this column directly (no slug column) but my order creation dies with the following errors:

["Number can't be blank", "Number is too long (maximum is 255 characters)"]

Am I able to deactivate or conditionally skip these validations?

@norman
Copy link
Owner

norman commented Feb 9, 2010

No, this isn't possible with FriendlyId right now.

Why you are using FriendlyId on a unique numeric column with no slugs? I think you are probably going to have problems, because your app at some point is not going to know whether you are looking for the record by its id or its friendly_id:

Model.find("100") # is this the friendly_id or the real id? I don't know.

In this case, FriendlyId will look for a record with FriendlyId "100" and if it's not found, it will fail over and look for a record with id 100, which will almost certainly cause problems for your application. If you want to avoid this failover behavior, you would need to use slugs.

@laserlemon
Copy link
Contributor Author

It's not a numeric value. An order number could be "AW123456" or similar. What I mean to point out is that its generation depends on the ID value.

@norman
Copy link
Owner

norman commented Feb 10, 2010

Ah, ok, I misunderstood.

Still, it's not going to be possible with the current FriendlyId. I will likely be changing FriendlyId to generate the slug on before_validate rather than before_save, which would solve problems like this. But I probably won't do this for a few more weeks.

If I were you, I would install friendly_id as a plugin or vendored gem for now, and just make a small patch to remove the validations.

@laserlemon
Copy link
Contributor Author

For now, I've reverted back to the previous version and I'll wait for a resolution. Thanks.

@laserlemon
Copy link
Contributor Author

It seems this isn't resolved, but the ticket's closed. Have you decided not to change this behavior?

@norman
Copy link
Owner

norman commented Mar 22, 2010

In fact, I was not intending to change it. However, I am now working on another feature that would allow models that use slugs to leave the slugs nil. I intend to generalize this functionality to non-slugged models, which would skip the validations when the friendly_id text is nil. This should allow you to do what you are trying to do.

I expect to release this feature later this week.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants