view by newspaper category with rails 4 -
my problem need fill array rails 4 display top 3 results categorized newspaper
category of newspaper is: 0,1,2,3 or other, random.
parsed_json
: 1 array values de newspapers, after news_source fill news newspapers.
sql: http://i.stack.imgur.com/dp8bm.png
with code last value of array of newspaper.
def index if get_api_key @parsed_json = array.new @news_source = array.new #trae todos los datos asociados la llave @emission = emission.where(key: get_api_key) #envío de datos json @emission.each |p| @weather_now = weathernowuy.where(city: p.state) @weather_next_days = weathernextdaysuy.where(city: p.state) @parsed_json << json.parse(p.news_source) end @parsed_json.each |u| @news_source = newsuy.where(newspaper: u).limit(3) end end end
example of return, no show newspaper 1 found in array parsed_json
.
not
"news": [ { "title": "con un gol de tevez, boca venció godoy cruz 2-0 y sigue en lo más alto", "description": "el delantero marcó de penal en la victoria en la bombonera. meli habÃa abierto el marcador. los \"xeneizes\" comparten con san lorenzo la primera posición.", "newspaper": "0" }, { "title": "barcelona picó en punta", "description": "la primera fecha del fútbol español se terminará el lunes con el partido entre granada y eibar.", "newspaper": "0" }, { "title": "sampdoria, chievo verona y fiorentina los lÃderes", "description": "este fin de semana se disputó la primera fecha de la serie italiana.", "newspaper": "0" }
] }
change @news_source = newsuy.where(newspaper: u).limit(3)
@news_source << newsuy.where(newspaper: u).limit(3)
in last @parsed_json.each do
loop. because, if use =
operator, replace every time next value , that's why getting last value. values, need accumulate @news_source
array using <<
operator.
so, need this:
@parsed_json.each |u| @news_source << newsuy.where(newspaper: u).limit(3) end
Comments
Post a Comment