ruby on rails - Scope or instance variable -


I've read using a scope in the rail, which, for example, to query directly as opposed The more efficient the model goes through the controller (how does it see how I see?)

Then I have this query in my controller

  @animal_location = User.select (: city) .map (& amp; city) .Nok ["Cardiff", "Newport"]  

And then in my model this scope

< Pre> area: uniq_town S (- "city") group ("city")} # and active framework: relations [# & lt; User ID: Zero, City: "Cardiff">, & lt; User ID: Zero, City: "Newport"> gt]] In my opinion, to reach the value of the city using the

@animal_location = user.select (: city) .map (& amp; city). > I can access such as

  & lt;% @ animal_location.each do | Loc | & Gt%; & Lt;% = loc% & gt; & Lt;% end% & gt;  

Or if I used the scope and went with @animal_location = User.uniq_towns , in my opinion I

  & Lt;% @ animal_location.each do | Loc | & Gt%; & Lt;% = loc.town% & gt; & Lt;% end% & gt; My first question is that on this occasion my circle will be sharp and the other thing is that my radius is correct because I am getting user ID: zero as part of the helle  

Thanks

This depends on the meaning of your source "efficient". According to a sense of "repeat themselves" scope, the codes are prepared to keep DRY, and for ActiveRecord (AR) questions, implement a "remove method" refactoring pattern, they are easy to maintain.

Why use it? If you use the same query in multiple places, consider the situation when you have to change it everywhere. You need to find and replace all the incidents in such a way that the error is prone, and version control causes more discrepancies, which are difficult to keep track of.

To remove this query in a method, it seems like a logical solution to the class method? , but using the just scroll bus scope, you will do the following:

  • Get a place to prepare a query with a certain meaning (And use it on the entire app)
  • Make sure that the result is a radius chain, because the result is a valid AR relationship (and you can apply scope at the top)
  • Meaning of your query to keep the controller code readable Provide a good readable name (use of scope in ideas is valid, but which follows the rail against MVC)

    You have:

      @animal_location = user.select (: town) .map (& amp; city first and foremost, I failed to see why the  map  and  group  required It works fine and uses SQL  DISTINCT : 

      @animal_location = user.select (: city) .Young  

    to be readable Looks adequately, but we display the scope here. In the case of your model it is:

      area: uniq_towns, - & gt; () {Select ("City"). Uniq}  

    ... so you can write it in your controller:

      @animal_location = User.uniq_towns  

    There are other ways to deal with this situation, such as before_filter , but it is beyond the scope of the question.


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -