S
S
sergenikitin2014-11-28 16:19:21
Ruby on Rails
sergenikitin, 2014-11-28 16:19:21

What does the RubyMine warning mean: controller action should call one model method other than an initial find or new.?

The controller has a method:

def create_country
    @country = Country.new(country_params)
    begin
      #сохраняем модель
      @country.save
      #это метод другой модели
      CountryTvText.create_or_update_for_country @country.id, params[:country_tv_text]
      flash[:notice] = "Страна добавлена"
    rescue Exception => e
      flash[:error] = e.message
    end
    redirect_to admin_panel_path
  end

And RubyMine highlights two lines with a warning: controller action should call one model method other than an initial find or new
@country.save
CountryTvText.create_or_update_for_country @country.id, params[:country_tv_text]

What am I doing wrong? :)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Viktor Vsk, 2014-11-28
@viktorvsk

Don't do this in the first place:
And after you rewrite it to something like

if @country.save
...
else
...
end

You look, and the error will disappear by itself)
Well, as for rubymine, although I don’t use it, it seems that the point is that it sees that under certain conditions your method will consist of object initialization and no further actions with it (apparently, when the exception will silently pass by). But here I could be wrong.

S
Sergey Krasnodemsky, 2014-12-01
@Prognosticator

controller action should call one model method other than an initial find or new
This is a warning that you are using more than one model method in a controller.
Read "Rails best practices"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question