Moving a request from a view to a model

I have two loose models - stages and users. (My milestones are actually owned by companies, and companies have many stages.)

Each milestone has a user who is responsible for this - in my stage form, I use the following to find and select users:

<%= f.input :milestone_user, :as => :select, :collection => User.find(:all, :order => "name ASC") %> 

This gives me user_id, which I converted in one view to a name as follows:

 <%= User.find(milestone.milestone_user).name %> 

This works fine, but I want to use it in several views now and don’t like having a query in my views.

I tried moving it to my User model, but I don't know how to do it. I tried this in the model:

  scope :username, lambda { where("id = milestone_user")} 

And this is in my opinion:

 <%= User.username.first_name %> 

But he complains about the undefined method for first_name ..

Thanks in advance

+7
source share
4 answers

Add this to your Milestone model:

 def username User.find(milestone_user).try :name end 

and use it like this in your opinion:

 <%= milestone.username %> 

But this is not effective, the best way is to stick belongs_to :user, :foreign_key => "milestone_user", :class_name => "User" in your Milestone model. You will not need to fill out the association, you can still create milestones without a designated user.

belongs_to will also allow you to use this code in your views:

 <%= milestone.user.name %> 

without creating additional methods for each custom property that you want to access.

+5
source

Move the request to the action of the appropriate controller:

 @milestone_user=User.find(milestone.milestone_user) 

And then use try in the view

 <%= @milestone_user.try(:name) %> 
+1
source

set the model to default_scope => :order('name asc')

In your controllers do @users = User.all

In your views use collection_select with @users

eg. collection_select (: milestone ,: user_id, @users ,: id ,: name)

For more information see http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select .

collection_select, grouped_collection_select, grouped_options_for_select and other helpers that have options such as

'grouped_collection_select (object, method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {})'

+1
source

I think associations should be like this:

 class Milestone < ActiveRecord::Base belongs_to :company has_one :user end class Company < ActiveRecord::Base has_many :milestones end class User < ActiveRecord::Base has_one :milestone end 

if you create an association as higher than on the view side, you can simply write

 <%= @milestone.user.firstname%> 

Otherwise, you can simply change the scope:

  scope :milestone_user, lambda { |user_id| where("id = ?",user_id) } 

Here I just changed the name of the area.

and a call from the view via parameter passing.

 <%= User.milestone_user(milestone.milestone_user).first_name %> 

You can also use delegate methods.

0
source

All Articles