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 8333795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:08:43+00:00 2026-06-09T03:08:43+00:00

I have an app that allows users to add courses to their profile. This

  • 0

I have an app that allows users to add courses to their profile. This is done through my ‘lineups’ model. To give you an idea of my database and schema:

#relationships
Class User
  has_many :lineups
end 

Class Course
  has_many :lineups
end

Class Lineup
  belongs_to :user
  belongs_to :course
end 

#some schema
create_table "lineups", :force => true do |t|
    t.integer  "course_id"
    t.integer  "user_id"
end

Now, I have a page were courses are listed with an ‘add course’ button. When clicked, a form submits to my ‘Lineup’ controller to the ‘create’ action. This process works great, however, my code that checks to see if a user has already added a course fails. The course will be added again. Here is my controller code:

class LineupsController < ApplicationController
    def create 
        course_id = params[:course_id]
        user_id = current_user.id
        course_object = Course.find(course_id) 

            #THIS BLOCK DOES NOT WORK
        for lineup in current_user.lineups do
            if lineup.course.id == course_id
                return redirect_to course_query_url, :alert => "You have already added this course: #{course_object.cl} #{course_object.cn}" 
            end
        end
            #THE BLOCK ^^^ DOES NOT WORK

        new_lineup = Lineup.new( course_id: course_id, user_id: user_id)

        respond_to do |format|
            if new_lineup.save 
                format.html { redirect_to course_query_url, :notice => "Course added: #{course_object.cl} #{course_object.cn}" }
            else
                format.html { redirect_to course_query_url, :alert => "There was an error in adding the course: #{course_object.cl} #{course_object.cn}" }
            end
        end
    end

end

I know that this line works, b/c I have it working in a view:

for lineup in current_user.lineups do

And I know that when the above line is iterated, the ‘lineup’ instance is created and the relationships work find (ie ‘lineup.course.id’) b/c it is also used in a view. Here is the code from the view in which this loop works:

      <% if current_user.lineups %>
        <% for lineup in current_user.lineups do %>
          <li><%= link_to "#{lineup.course.id} #{lineup.course.cn}", index_path %></li>
        <% end %>
      <% end %>

I am really at my wits end here and cannot figure out why this conditional is not working. Any and all input would be appreciated.

  • 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-09T03:08:44+00:00Added an answer on June 9, 2026 at 3:08 am

    Your are comparing number (course.id) and string (params[:id]) in this line

    if lineup.course.id == course_id
    

    This does not work in Ruby 🙂

    Try to change to

    if lineup.course.id.to_s == course_id
    

    or

    if lineup.course.id == course_id.to_i
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an app that allows users to upload large data files and then
I have an iPhone app that allows users to record videos and I'd like
I have a topics app that allows users to create topics. For each individual
We have a WPF app that allows our users to download encrypted content and
I have an app that allows the user to choose a picture from their
I have a Silveright app that allows users to specify filters on a few
I am wanting to have an In-App Purchase that allows my users to import
I want to make an app that allows users to add other users to
I have built a ruby on rails app that allows for users to track
I have built a Ruby on Rails app that allows users to track workouts.

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.