Sunday, 15 February 2015

ruby on rails - avoid issue in the mvc -



ruby on rails - avoid issue in the mvc -

i know view shouldn't have knowledge, of model or controller not sure how avoid it.

the issue trying fetch info controller or model before need manipulate in view.

here code

model user location article id user_id title first article_id ... lastly ...

relationship

user has many locations article has many locations location belongs user location belongs article

controller

@userlocation = @user.locations

view

<% @userlocation.each |event| %> ... <% evtitle = article.find_by_id(event.article_id) %> <%= evtitle.title %> <% end %>

obviously not best not sure how query in controller, , evtitle doesnt study why?

in controller can this:

@userlocations = @user.locations @locations_articles = article.find_all_by_id(@userlocations.map(&:article_id).uniq)

then can utilize @locations_articles in view. avoid using db queries in view.

<% @locations_articles.each |article| %> <%= article.title %> <% end %>

ruby-on-rails ruby ruby-on-rails-3 model-view-controller

No comments:

Post a Comment