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
Or if I used the scope and went with 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: You have: to be readable Looks adequately, but we display the scope here. In the case of your model it is: ... so you can write it in your controller: There are other ways to deal with this situation, such as @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;
@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
@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
area: uniq_towns, - & gt; () {Select ("City"). Uniq}
@animal_location = User.uniq_towns
before_filter
, but it is beyond the scope of the question.
Comments
Post a Comment