Comments (8)
That's an issue with uniqueness
, because it checks at the database column level. Globalize required a hack to get that to work (I actually built that hack, so I know it well).
If you remove the uniqueness: true
it works for presence only, right?
from mobility.
Here's the PR on Globalize: globalize/globalize#121
I don't want to do this type of hack within Mobility. Maybe create an external gem which you'd have to include, say mobility-uniqueness
or something. Need to think about it.
I'm removing the "bug" label because I don't consider this a bug, but a feature request.
from mobility.
Thanks for quick response.
It seems that more compatible works need to do if friendly_id , globalize, mobility gem work together.
from mobility.
So I checked this quickly. Presence validation works fine as expected. Uniqueness does not work. Depending on the backend, it will either not work as expected, or it will raise an error (complaining that a column does not exist).
It seems that more compatible works need to do if friendly_id , globalize, mobility gem work together.
Are you using globalize and mobility together? That seems a bit strange.
About compatiblity, the only issue I see here is with uniqueness validation. Are there any other issues you are experiencing?
from mobility.
Are you using globalize and mobility together? That seems a bit strange.
About compatiblity, the only issue I see here is with uniqueness validation. Are there any other issues >you are experiencing?
Yes. I'd like to look into which gem is suitable to our application.
from mobility.
I see. Well, the uniqueness problem can be solved, as a plugin or extension to Mobility.
For now, if you want a quick fix, you can add this class somewhere in your application:
class MobilityUniquenessValidator < ::ActiveRecord::Validations::UniquenessValidator
def validate_each(record, attribute, value)
klass = record.class
if klass.translated_attribute_names.include?(attribute.to_s)
relation = klass.i18n.where(attribute => value)
relation = relation.where.not(klass.primary_key => record.id) if record.persisted?
relation = relation.merge(options[:conditions]) if options[:conditions]
if relation.exists?
error_options = options.except(:case_sensitive, :scope, :conditions)
error_options[:value] = value
record.errors.add(attribute, :taken, error_options)
end
else
super
end
end
end
Now in your models, before you call validates
, just add this line:
class UniquenessValidator < ::MobilityUniquenessValidator; end
This is a very basic uniqueness validation. I should work with any backend other than the serialized backend, but without the scope
option.
from mobility.
Basic uniqueness validation, including scopes, should work now using master
as long as you include Mobility
before adding validations to your model.
from mobility.
not working in rails 7 properly.
We want the model to validate the presence of all locales (name_en, name_fr, etc)
This works:
validates :name_en, :name_fr, presence: true
This doesn't:
validates :name_en, :name_fr, presence: true, uniqueness: true
But this works when we use only name
:
validates :name, presence: true, uniqueness: true
from mobility.
Related Issues (20)
- Use mobility with StoreModel HOT 2
- Issue with backend extention HOT 1
- reader & locale_accessors won't return correct translation, but backend_reader does HOT 5
- Destruction of DB records with linked translation records fails (with a strange SQL query) HOT 2
- Model attribute defined fallback HOT 2
- Error when ordering and selecting distinct on translated resource HOT 2
- Marks object as dirty even if nothing was changed in RichTextTranslation fields HOT 5
- Cache issue with JSONb (+locale_accessors) & ActiveRecord HOT 2
- Using a query method block does not automatically scope to i18n HOT 1
- Translated inputs are empty after failed form validation in a specific setup HOT 1
- Automatic ActionText `rich_text` detection failing when operating on locale-dependent mobility field HOT 1
- Any accepted way to store images working with active storage ? HOT 1
- Unique Validation didn't work with locale_accessors on every locales HOT 1
- Not compatible with FastGettext - request for string available locale support HOT 2
- Error when installing on Vitess MySQL db HOT 4
- The presence plugin doesn't work as expected HOT 1
- Destruction of record+translations fails, when model IDs are of the type "varchar(36)" (a UUID string) HOT 3
- accessor super method doesn't work with arguments like class.value(locale: :de) HOT 1
- ordering by specific locale HOT 2
- Add type checking plugin reference HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mobility.