Loop Within Loop in Rails Controller

Loop within Loop in Rails Controller

You are overwriting @posts with each iteration.
Try this:

def index
@institution = Institution.find(current_user.institution.id)
@categories = Category.all
@posts = []
@categories.each do |category|
tposts = Post.where("category_id = ? and institution_id = ?", category, @institution).order("created_at DESC")
@posts += tposts if tposts
end
authorize! :read, @post
respond_with(@posts)
end

To retrieve all posts with non null category_id, try this:

def index
@institution = Institution.find(current_user.institution.id)
@categories = Category.all
@posts = Post.where("category_id is not null and institution_id = ?", @institution).order("created_at DESC")
authorize! :read, @post
respond_with(@posts)
end

Change is not null to > 0 for integer category_id or != '' if your table contains '' instead of nulls.

Good luck.

Rails 4: How to put loop logic inside the controller

You can't render the view multiple times but you could do something like this.

def index
@loops = [
Item.where(featured: true).order("RANDOM()"),
Item.order(:cached_votes_up => :desc),
Item.order(:impressions_count => :desc),
Item.order("created_at DESC")
]
end

And then in the template

- @loops.each do |loop|
- loop.limit(4).each do |item|
%li
%h1 This is an example

Ruby Rails Loop in Controller

You do not seem to be populating your @cs_employees variable when you call @cs_employees.each

Also, there is way too much business logic in your controller. Move it to your model or a concern.

Instance Variable loop inside the controller

A better way to do this is to simply fetch the records from the db and store the whole collection in one instance variable.

@stores = Store.where(user_id: [1, 2, 3])

@markers = Gmaps4rails.build_markers(@stores) do |store, marker|
marker.lat store.lat
marker.lng store.long
marker.title store.name
marker.infowindow "#{store.store_infowindow}"
# if they need different pictures handle it in the model
marker.picture({
:url => "//chart.apis.google.com/chart?chst=d_map_pin_letter&chld=TP|81DF08|000000",
:width => 52,
:height => 32
})
end

Doing this:

  @rep1 = Store.where(:user_id => "1")
@rep2 = Store.where(:user_id => "10")
@rep3 = Store.where(:user_id => "11")
@rep4 = Store.where(:user_id => "12")
@rep5 = Store.where(:user_id => "13")
@rep6 = Store.where(:user_id => "14")
@rep7 = Store.where(:user_id => "15")

Is horrible for performance since each line will create a separate database query. If you are new at ruby I would suggest doing something like http://tryruby.org and learning how to manipulate arrays and hashes and the basics before trying to solve more complex problems.

Nested Each Loops Understanding

The below code should do

- @articles.each do |article|
= article.text
#level of indentation matters.
- article.comments.each do |comment| #this will loop through respected article's comments
= comment.text

How to loop through nested array in rails

I assume that SupplierItem object attributes should be taken from item_ids, location_ids and supplier_prices arrays by the same index.

The code:

@supplier = Supplier.new(supplier_params)
if @supplier.save
item_params = params.require(:supplier_item).permit(item_ids: [], location_ids: [], supplier_prices: [])
# => {"item_ids"=>["2", "4"], "location_ids"=>["1", "2"], "supplier_prices"=>["7.8", "45.0"]}
items = item_params[:item_ids].zip(*item_params.values_at(:location_ids, :supplier_prices))
# => [["2", "1", "7.8"], ["4", "2", "45.0"]]
items.map do |item_id, location_id, supplier_price|
supplier_item = SupplierItem.new({
item_id: item_id,
location_id: location_id,
supplier_price: supplier_price,
supplier_id: @supplier.id,
}.compact)
supplier_item.save # TODO: check if it saved
end
flash[:success] = "New supplier created"
redirect_to supplier_path(@supplier)
else
render 'new'
end

PS: Don't use float/double for money.



Related Topics



Leave a reply



Submit