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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:55:21+00:00 2026-06-11T09:55:21+00:00

I have two models: Client and Invoice. Every client can have many invoices, every

  • 0

I have two models: Client and Invoice.
Every client can have many invoices, every invoice belongs to only one client. If the client is deleted, the invoices with reference to him should be deleted, too.

This is all done through the following code:

#### Invoice class
class Invoice < ActiveRecord::Base
  attr_accessible :amount, :body, :client_id, :filename, :subject

  validates :amount, :body, :client_id, :filename, :subject, :presence => true
  validates :client_id, :numericality => { :only_integer => true, :greater_than_or_equal_to => 0 }

  belongs_to :client
end

#### Client class
class Client < ActiveRecord::Base
  attr_accessible :city, :country, :name, :street, :zip

  validates :city, :country, :name, :street, :zip, :presence => true
  validates :zip, :numericality => { :only_integer => true, :greater_than_or_equal_to => 10000, :less_than_or_equal_to => 99999 }

  has_many :invoices, dependent: :destroy
end

This is what I have built so far – but I am wondering: How can I validate that when a user creates a new invoice, the client id in the client table actually exists, and if not, display an according error message?

  • 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-11T09:55:22+00:00Added an answer on June 11, 2026 at 9:55 am

    For foreign key (FK) constraints, I would recommend you do that in your database.
    Rails itself has no built-in support for that. If you do want to check foreign keys’ existence in Ruby/Rails, that would add unnecessary load to the application.

    Here are several links might help:

    You Should Use Foreign Key Constraints in Rails

    Support for foreign key constraint in Rails

    Adding foreign key constraints in rails migrations

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models, Reports(belongs to client) and Clients(has many reports). A Client has
I have two Models, Programme and Event, a programme has many events. I need
I have two models, A and B, and one light, L. I would like
I have two models Clients and Reports. A Client has_many Reports and a Report
I have two models. I am trying to query a model for how many
I have two models, Client and PaymentOptions . class Client(models.Model): name = models.CharField(max_length=50, null=True,
I have two models, Client and PaymentOptions . class Client(models.Model): name = models.CharField(max_length=50, null=True,
I have two models jobs and clients . A user can simple create a
I have a Django project with two models: Applicant and Client, where Client is
I have two models Client and Topic . Both of which have a HABTM

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.