Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving Query from View to Model

I have two non-attached models - milestones and users. (My milestones actually belong to companies and companies have many milestones.)

Each milestone has a user who'd responsible for it - in my milestone form, I'm using the following to find and select users:

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

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

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

This works fine but I want to use this in a few views now and don't like having the query in my views.

I have tried to move it into my User model but I don't know how to go about it. I've tried this in the model:

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

And this in my view:

<%= User.username.first_name %>

But it complains about an undefined method for first_name..

Thanks in advance

like image 800
Jenny Blunt Avatar asked Aug 27 '11 11:08

Jenny Blunt


People also ask

How do I export a query from Power Query?

Exporting queries in Excel to a Power Query template Start the Power Query editor from Data tab > Get Data > Launch Power Query Editor. Once Power Query loads, select File > Export Template. The template requires basic information such as a name and a description before it can be saved locally on your computer.

What is model query?

Model query subjects are not generated directly from a data source but are based on query items in other query subjects or dimensions, including other model query subjects. By using model query subjects, you can create a more abstract, business-oriented view of a data source.

How do I open the query and connections pane in Excel?

In Excel Select Data > Queries & Connections > Queries tab. In the Power Query Editor Select Data > Get Data > Launch Power Query Editor, and view the Queries pane on the left.


3 Answers

Add this to your Milestone model:

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

and use it like this in your view:

<%= milestone.username %>

But this isn't efficient, the better way would be to stick belongs_to :user, :foreign_key => "milestone_user", :class_name => "User" in your Milestone model. You won't have to fill the association, you still could create milestones without an assigned user.

The belongs_to would also allow you to use code like this in your views:

<%= milestone.user.name %>

without having to create additional methods for every user property that you'd like to access.

like image 192
Benoit Garret Avatar answered Sep 21 '22 23:09

Benoit Garret


Move the query in the action of the related controller:

@milestone_user=User.find(milestone.milestone_user)

And then use try in the view

<%= @milestone_user.try(:name) %>
like image 34
lucapette Avatar answered Sep 22 '22 23:09

lucapette


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

In your controllers do @users = User.all

In your views use collection_select with @users

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

See http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select for more info about

collection_select, grouped_collection_select, grouped_options_for_select and other helpers which have options like

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

like image 43
Michael Durrant Avatar answered Sep 22 '22 23:09

Michael Durrant