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

  • Home
  • SEARCH
  • 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 7914437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:06:54+00:00 2026-06-03T14:06:54+00:00

A basic overview of my app. There is currently two models. A jobs model

  • 0

A basic overview of my app. There is currently two models. A jobs model and a clients model. Both models have a has_and_belongs_to_many relationship as I intended to allow the user to create a client entry and then assign them one or many jobs.

Here are both of my models.

Clients –

class Client < ActiveRecord::Base 
  has_and_belongs_to_many :job
end

Jobs –

class Job < ActiveRecord::Base
  has_and_belongs_to_many :client
end

I have been doing some research and I think im right in thinking that the relationship needs a foreign key to function so have added a client_id column & a job_id column to my database.

The clients page is currently working and here is my controller for that.

class ClientsController < ApplicationController

        def index
          @clients = Client.all

          respond_to do |format|
            format.html # index.html.erb
            format.json { render json: @clients }
          end
        end

        # GET /Clients/1
        # GET /Clients/1.json
        def show
          @clients = Client.find(params[:id])

          respond_to do |format|
            format.html # show.html.erb
            format.json { render json: @clients }
          end
        end

        # GET /Clients/new
        # GET /Clients/new.json
        def new
          @clients = Client.new

          respond_to do |format|
            format.html # new.html.erb
            format.json { render json: @clients }
          end
        end

        # GET /Clients/1/edit
        def edit
          @clients = Client.find(params[:id])
        end


        def create
          @clients = Client.new(params[:client])

          respond_to do |format|
            if @clients.save
              format.html { redirect_to @clients, notice: 'Client was successfully created.' }
              format.json { render json: @clients, status: :created, location: @clients }
            else
              format.html { render action: "new" }
              format.json { render json: @clients.errors, status: :unprocessable_entity }
            end
          end
        end

        # PUT /Clients/1
        # PUT /Clients/1.json
        def update
          @clients = Client.find(params[:id])

          respond_to do |format|
            if @clients.update_attributes(params[:client])
              format.html { redirect_to @clients, notice: 'Client was successfully updated.' }
              format.json { head :no_content }
            else
              format.html { render action: "edit" }
              format.json { render json: @clients.errors, status: :unprocessable_entity }
            end
          end
        end

        # DELETE /Clients/1
        # DELETE /Clients/1.json
        def destroy
          @clients = Client.find(params[:id])
          @clients.destroy

          respond_to do |format|
            format.html { redirect_to :clients , notice: 'Client was successfully removed.'}
            format.json { head :no_content }
          end
        end

        def details
            @clients = Client.find_by_id(params[:id])
            @jobs = Client.job
        end
      end

And here’s what I currently have for my jobs controller.

class JobsController < ApplicationController

  def index
    @jobs = Job.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml { render :xml => @job }
    end
  end

  def new
    @jobs = Job.new 

    respond_to do |format|
      format.html # index.html.erb
      format.xml { render :xml => @job }
    end
  end

  def create
    @jobs = Job.new(params[:job])
    @cients = Client.find = Client.find(params[:id])

    respond_to do |format|
      if @jobs.save
        format.html { redirect_to(@jobs,
                      :notice => 'Job was successfully created.') }
        format.xml  { render :xml => @jobs,
                      :status => :created, :location => @Job }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @jobs.errors,
                      :status => :unprocessable_entity }
      end
    end
  end
end

In my jobs form I was given thr following code which added a drop down with all the created clients.

<%= select("job", "client_id", Client.all.collect {|c| [ c.name, c.id ] }, {:include_blank => 'None'})%> 

When I press save though. I recieve the following error.

unknown attribute: client_id

Application Trace | Framework Trace | Full Trace
app/controllers/jobs_controller.rb:22:in `new'
app/controllers/jobs_controller.rb:22:in `create'

I assume this is because I need to define a way of finding the client_id in my job creation as well as specifying one in my client creation.

This is my first rails app though so im not quite sure how.

Any help would be greatly 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-03T14:06:56+00:00Added an answer on June 3, 2026 at 2:06 pm

    Your jobs table doesn’t have a client_id, nor should it. You need to create a junction table to facilitate a many-to-many relationship. It should be called clients_jobs and contain an integer client_id and job_id.

    There is a lot more wrong here. Here are just the things I caught at a casual glance:

    1. This line:

      @cients = Client.find = Client.find(params[:id])
      

      should be:

      @cients = Client.find(params[:id])
      
    2. Pluralization is important in Rails. A client doesn’t have many “job”. It has many jobs. Your models should reflect this:

      class Client < ActiveRecord::Base 
        has_and_belongs_to_many :jobs
      end
      
      class Job < ActiveRecord::Base
        has_and_belongs_to_many :clients
      end
      
    3. You’ll need to create a junction table via a migration, which is where your foreign keys will exist:

      $ rails g migration AddClientsJobsTable 
      
    4. In index and new, you first create @jobs = Job.new and then you render it via :xml => @job. Again, pluralization is important. You need @job = Job.new. You have the same problem in create, except you’ve dropped the ‘s’ and capitalized the ‘J’: :location => @Job } You can’t do that in programming. Case and spelling both matter.

    5. Job.find(:all) or Client.all: Pick one. Don’t mix find :all and .all.

    6. @clients = Client.find(params[:id]). You’re finding a single specific Client, not a collection of clients. Your variable should be called @client. This is not an error, but it is seriously ugly.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Overview I have a Microsoft Word Add-In, written in VBA (Visual Basic for Applications),
basic overview... i have a site setup in iis... - mysite (wwwroot\mysite) under that
Basic C# syntax question: So I have this class public class BrandQuery<T> : Query<T>
basic c++ question i'm fairly sure. if i have a base class with a
Basic question! I have 2 tables PRODUCE +-----+--------------+ | id | fruit_name | +--------------------+
I'm looking at restructuring a large Maven project... A basic overview of our current
Overview: I'm working on some windows service using Visual Basic 2010, which deployed on
I am looking for quick reference guide(s) for both OO and C++. I have
Here's a basic overview of my domain: a user has a list of courses
Overview I am trying to create a very basic scraper with PhantomJS and pjscrape

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.