Conditional statement fails Rails 4 -
i developing rails app has pins assigned category when created. app has 16 categories in total. pins displayed on main index page ( except category 16 ) , user can click pin take them show page of @pin. show page has @pin , random pin selected pins in db ( except category 16 ). mentioned actions above defined in pins controller , function without error.
the problem when category 16 selected , user proceeds pins show page random pin should pin category 16 only, being selected pins in db!
i have set variables , conditional statements in index, , show actions achieve above, please check comments in pins controller show-action. problem code persists @ comment ====something wrong!====
please relatively new rails i'm not sure doing wrong, have spent hours trying sorts nothing has worked.
pins_controller index action
def index if params[:category].blank? @restricted = category.find(16) # category id 16 set var @restricted used exclude cat 16 index page in line below. @pins = pin.where.not(category: @restricted).all.order("created_at desc").paginate(page: params[:page], per_page: 20) else @category_id = category.find_by(name: params[:category]).id @pins = pin.where(category_id: @category_id).order("created_at desc").paginate(page: params[:page], per_page: 20) end pins_controller show-action problem
def show @pin.increment_view_count @show_main_cat = category.where.not(16) # categories except category id 16 set var @show_main_cat if @show_main_cat.present? @restricted = category.find(16) # category id 16 set var @restricted. @random_pin = pin.where.not(id: @pin, category: @restricted).order("random()").first # select random pin categories except 16 + not @pin. end @show_restricted_cat = category.find(16) # category 16 set var @show_restricted_cat < ==========something wrong!============== if @show_restricted_cat.present? @restricted = category.where.not(16) # categories except 16 set var @restricted. @random_pin = pin.where.not(id: @pin, category: @restricted).order("random()").first # select random pin category 16 + not @pin. end @reviews = review.where(pin_id: @pin.id).order("created_at desc").limit(5) if @reviews.blank? @avg_review = 0 else @avg_review = @reviews.average(:rating).round(2) end
this line not expected
@random_pin = pin.where.not(id: @pin, category: @restricted).order("random()").first # select random pin category 16 + not @pin. it select random pin category 16 or id not @pin (what expect think from category 16 , id not @pin). change to:
@random_pin = pin.where.not(category: @restricted).where.not(id: @pin).order("random()").first have fun!
Comments
Post a Comment