I recently saw some Rails code where a model was called from a helper function. I was wondering if that is allowed by MVC? Right now I''m working on this: - my controller finds a list of projects... @projects = Project.find(:all, :order => "name ASC") - ...it passes @projects to the view which passes it to a partial as a collection... render(:partial => "project_row", :collection => @projects) - the partial render a <tr> containing this... <tr> <% @day.upto(@day+13) do |d| %> <%= %{<td id="project_#{project_row.id}_day_#{d.strftime "%m_%d_ %y"}"> <div>DES: {NO OF ''DES'' HERE}</div> <div>DEV: {NO OF ''DEV'' HERE}</div> <div>PMG: {NO OF ''PMG'' HERE}</div> </td>} %> <% end %> </tr> - ...I''m tempted to find the no. of DES/DEV/PMG by calling a helper function which will call the model and query the table for one entry on the right date, for the right project and for the right category (DES, DEV or PMG). It would work but I''m not sure if this is how it is supposed to be done under MVC. Maybe someone can give me a hint as to the best way to do the above in Rails / MVC? Thanks, Gabor --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
gabordemeter wrote:> I recently saw some Rails code where a model was called from a helper > function. I was wondering if that is allowed by MVC? > > Right now I''m working on this: > > - my controller finds a list of projects... > > @projects = Project.find(:all, :order => "name ASC") >Hi, You cannot access the models from the helper, so this is not allowed. I don''t see any problem with the way you are doing it :) Regards, Jamal -- Posted via ruby-forum.com. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks for the feedback Jamal. Please note that you can access the models from both helpers and views. I just tested, by doing: def test Project.find(:first).id end in my helper. I was then able to display the project id in my view by calling the test method. So it definitely can be done. The question is...should it be done? And if not, what''s the MVC friendly way of doing this? Best, Gabor On Jun 1, 1:05 pm, Jamal Soueidan <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> gabordemeter wrote: > > I recently saw some Rails code where a model was called from a helper > > function. I was wondering if that is allowed by MVC? > > > Right now I''m working on this: > > > - my controller finds a list of projects... > > > @projects = Project.find(:all, :order => "name ASC") > > Hi, > > You cannot access the models from the helper, so this is not allowed. > > I don''t see any problem with the way you are doing it :) > > Regards, > Jamal > > -- > Posted viahttp://www.ruby-forum.com.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
gabordemeter wrote:> Thanks for the feedback Jamal. > > Please note that you can access the models from both helpers and > views. I just tested, by doing: > > > def test > Project.find(:first).id > end > > in my helper. > > I was then able to display the project id in my view by calling the > test method. > > So it definitely can be done. The question is...should it be done? And > if not, what''s the MVC friendly way of doing this? > > > Best, > > Gabor > > > > On Jun 1, 1:05 pm, Jamal Soueidan <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>My mistake sorry, I was testing if I could receive ids from User model which did not exist before. Anyway, you should not let the helper connect to your models, this should your controller do for you. This is how things work with MVC. Helper are made to cut things down for things in your views. For example you could make a method called h1, so you don''t need to create both tags def h1(text) "<h1>" + text + "</h1> end -- Posted via ruby-forum.com. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hard and fast rules like never accessing a model directly in a helper are more harmful than helpful. You need to gain experience to figure out what makes the most sense. For instance, I have many helpers that call on a model to generate a select menu. Calling the model in the controller is confusing and error-prone in that kind of case because it really is view-related logic. Each action shouldn''t have to know whether this select menu is even available. Keeping it in the helper is DRYest. However in your case Jamal is right. The extra data you are querying for is related to projects. Therefore you should define methods on your Project class that return the data you need. You also might be able to set up joins and whatnot so this data is prefetched for all projects and you don''t end up with 1+n queries. On Jun 1, 3:27 pm, Jamal Soueidan <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> gabordemeter wrote: > > Thanks for the feedback Jamal. > > > Please note that you can access the models from both helpers and > > views. I just tested, by doing: > > > def test > > Project.find(:first).id > > end > > > in my helper. > > > I was then able to display the project id in my view by calling the > > test method. > > > So it definitely can be done. The question is...should it be done? And > > if not, what''s the MVC friendly way of doing this? > > > Best, > > > Gabor > > > On Jun 1, 1:05 pm, Jamal Soueidan <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > My mistake sorry, I was testing if I could receive ids from User model > which did not exist before. > > Anyway, you should not let the helper connect to your models, this > should your controller do for you. This is how things work with MVC. > > Helper are made to cut things down for things in your views. > > For example you could make a method called h1, so you don''t need to > create both tags > > def h1(text) > "<h1>" + text + "</h1> > end > > -- > Posted viahttp://www.ruby-forum.com.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---