Friday, 15 August 2014

ruby on rails - How to order a table based on the value of an instance method of a separate but associated table? -


Using the rail, how can I order on the basis of another table based on the order table of a table?

For example:

Model / State .bb

  is_im: weather  

Model / weather.rb

  related_to: State Def Intensity # Finish some calculation  

Schema

  create_table "weathers" t.string "name" end  

So, in all we : states : with weather with name: "snow" . (They are with different values ​​of different records intensity , but they have the same name ). @ state.weathers.find_by (name: "snow"). Intensity ?

How can I list all the states sorted by "div class" itemprop = "text">

Just use joining:

< Pre> @states = State.joins (weather). Where ("weathers.name =?", "Snow") .sort_by do | X, y | X.weathers.map (and: intensity). Max. & Lt; = & Gt; Y.weathers.map (& amp;: Intensity). Max end

Sorry, you have to sort the data with Ruby. I sort with the maximum intensity because there may be more than one season for each of your states.


No comments:

Post a Comment