Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8068083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:32:30+00:00 2026-06-05T12:32:30+00:00

Ok, I know this is for the Saas course and people have been asking

  • 0

Ok, I know this is for the Saas course and people have been asking questions related to that as well but i’ve spent a lot of time trying and reading and I’m stuck. First of all, When you have a model called Movie, is it better to use Ratings as a model and associate them or just keep Ratings in an array floating in space(!). Second, here’s what I have now in my controller:

def index

@movies = Movie.where(params[:ratings].present? ? {:rating => (params[:ratings].keys)} :   {}).order(params[:sort])
@sort = params[:sort]
@ratings = Ratings.all

end

Now, I decided to create a Ratings model since I thought It would be better. Here’s my view:

= form_tag movies_path, :method => :get do

Include:
- @ratings.each do |rating|
 = rating.rating
 = check_box_tag "ratings[#{rating.rating}]"
= submit_tag "Refresh"

I tried everything that is related to using a conditional ternary inside the checkbox tag ending with ” .include?(rating) ? true : “” I tried everything that’s supposed to work but it doesn’t. I don’t want the exact answer, I just need guidance.Thanks in advance!

Update (the controller’s method):

Here is the index method within my controller that is reading this hash – for clarity. I apologize for the fuzziness before.

def index    
  @all_stores = Product.all_stores
  @selected_stores = params[:stores] || session[:stores] || {}

  if @selected_stores == {}
    @selected_stores = Hash[@all_stores.map {|store| [store, store]}]
  end
  if params[:stores] != session[:stores]
    # session[:stores] = @selected_stores
    session[:stores] = params[:stores]
    redirect_to :stores => @selected_stores and return
  end
  @products = Product.order("created_at desc").limit(150).find_all_by_store(@selected_stores.keys).group_by { |product| product.created_at.to_date}
. . . etc
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T12:32:31+00:00Added an answer on June 5, 2026 at 12:32 pm

    Couple of things

    1) .order(params[:sort]) opens you up for sql injection attacks,
    someone could potentially delete all of your users by putting this in the query string

    http://localhost:3000/movies?sort=email%3B+DELETE+from+users+--

    see rails 3 activerecord order – what is the proper sql injection work around?, this problem does not exist with .where, rails will sanitize the input for where method

    2) minor thing, AREL delays making the actual call to the db until you iterate on the collection, so you can ‘build’ up queries, this syntax might be easier to understand then using ternary operator?

    @movies = Movie.order(xxxxxx)
    @movies = @movies.where(:rating => params[:ratings].keys) if params[:ratings].present?
    

    3) for your ratings, like cdesrosiers says constant is fine, then your view

    Movie::RATINGS.each do |rating|
      check_box_tag "ratings[]", rating # <input type="checkbox" name="ratings[]" value="PG" />
    

    EDIT: maintain selected values

    # controller
    @selected_ratings = (params[:ratings].present? ? params[:ratings] : [])
    
    # view
    Movie::RATINGS.each do |rating|
      check_box_tag "ratings[]", rating, @selected_ratings.include?(rating)
      # <input type="checkbox" name="ratings[]" value="PG" checked="checked" />
    

    note the [] in the naming of the checkbox, this will give you params[:ratings] as an array in your controller action

    @movies = @movies.where("rating IN (?)", params[:ratings]) if params[:ratings].present? and params[:ratings].any?
    

    some links

    • http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-include-3F
    • http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-check_box_tag
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this question would have been asked several time but still I am
I know this is a stupid question, but I have been teaching myself c#,
I know this might have been asked a thousand times before, but I can't
I know this has been asked a lot around SO, but I just can't
Know this might be rather basic, but I been trying to figure out how
I know this is an old question, but I have spend any hours on
I know this question has been asked before but the solutions did not work
I know this question has been asked many times before, I have found a
I know this question has been asked many times here, but no answer has
I know this question has been asked a million times and I have seen

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.