I wrote this code in controller:
def list
@codes = Code.order("created_at")
@languages = Language.order('name').collect {|l| [l.name, l.coderay]}
@codes is an array of posts. Each code has language field for cpp or text string. It contains coderay token.
@languages is array of programming languages in format ['C++', 'cpp'], ['Plain Text', 'text'].
In other words, format of Language is :name, :coderay. I use it only in view to make select box.
So I use :coderay as primary key, but ruby added own PK :id to this model. And these models are not linked.
IDE writes me this warning:
Controller action should call one model method other than an initial
find or new
This inspection warns if a controller
action contains more than one model method call, after the initial
.find or .new. It’s recommended that you implement all business logic
inside the model class, and use a single method to access it
What is a best solution to solve this problem?
1) Add 1-to-m link between Codes and Language and make :coderay PK.
2) Ignore this warning
3) Move Language.order('name').collect {|l| [l.name, l.coderay]} to view.
I think the best solution is (1), how can I do this?
You can implement option 1 to add a 1-to-1 link between your
LanguageandCodeby using a belongs to, and setting the foreign key.code.rb
language.rb
However if you want to load static data for a select box, I usually prefer to do that from a before_filter in the controller and pass it across to the view.
You might also like the
decent_exposuregem. – https://github.com/voxdolo/decent_exposure