Forum: Ruby on Rails Custom validation and i18n

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1e782845563c051556143a36ee9cf365?d=identicon&s=25 Rasmus Nielsen (rasmus)
on 2009-04-01 17:07
Hi guys,

I'm looking for some advice on how to follow best practices. In my app
users can create "reports". A report belongs to an employee.

Therefore, when creating a report I want to check, that the chosen
employee actually exists and that the user are allowed to use this
employee. To do this, I've made a custom validation method (in the
report model), that checks whether the employee exists or not and
whether the user can use this employee. If something's wrong, it call
errors.add_to_base "some text". The problem is, that "some text" needs
to be translated according to what user is viewing the page. So
essentially, the model needs to have access to both action view methods
as well as the session (in order to determine current language). This is
afaik very bad practice (to give the model this access).

Another solution would be to call @report.errors_add_to_base from the
controller - however, that would break the idea of "thin controllers,
fat models". Also, it will lead to ugly such as

@report.errors.add_to_base t(:error_msg) if something_wrong?

if @report.validates? && save # (save seems to clear errors, so I need
to call validates before save)
 # succes
else
 # show error page
end

So, dear fellow rails users, what do you think of this problem? What
would you do here? Any advice very appreciated, thanks.
This topic is locked and can not be replied to.