V
V
Vasily Shakhunov2016-08-02 21:51:12
Ruby on Rails
Vasily Shakhunov, 2016-08-02 21:51:12

How to pull out the names of dependent models with errors?

There is a complex order form that belongs to the Client and the Address. Accordingly in the controller:

def new
    @order = Order.new
    @order.build_kls_client
    @order.build_kls_address
end

Since the Client and Address are in tabs, the task is to mark the corresponding tab if the corresponding model fails the validation. To do this, you need to understand in which dependent models of the error.
Wrote this line:
o.errors.messages.keys.collect(&:to_s).select { |v| v.split('.')[1] }.map { |x| x.split('.')[0]}.uniq

How close is it to shitcode? Is there an easier way to get these names?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question