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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:50:33+00:00 2026-05-10T21:50:33+00:00

I have two tables: Client(id,name,…) Purchase(id,item,date,client_id,…) They have their respective Model, with their validations.

  • 0

I have two tables: Client(id,name,…)
Purchase(id,item,date,client_id,…)

They have their respective Model, with their validations. What I need is to create a new client with a new purchase, all into the create method of Client controller. Something like this:

def create     @client = Client.new(params[:client])    respond_to do |format|     if @client.save       # Add purchase       @sell = Purchase.new       @sell.client_id = @client.id       @sell.date = params[:date]       # Fill another fields       if @sell.save          # Do another stuff...        else         format.html { render :action => 'new' }         format.xml  { render :xml => @client.errors, :status => :unprocessable_entity }       end        flash[:notice] = 'You have a new client!'       format.html { redirect_to(:action => :show, :id => @evento.id) }       format.xml  { render :xml => @client, :status => :created, :location => @client }     else       format.html { render :action => 'new' }       format.xml  { render :xml => @evento.client, :status => :unprocessable_entity }     end   end end 

In Purchase’s model I have:

belongs_to :client validates_format_of :date, :with => /^20[0-9]{2}[-][0-9]{2}[-][0-9]{2}$/, :message => 'not valid' validates_presence_of :date 

And there is my problem: how can I validate the date input, through validations into the model, from Client controller? And, how can I rollback the new client created when errors?

Yes, I can do the check as the very first instruction in the method, with a regular expression, but I think it’s ugly. I feel like might exist a conventional method for doing this validation or even doing all the stuff in another way (i.e. calling create method for Purchase from Client controller).

Can you put me back in the right way?

Thank you in advance.

  • 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. 2026-05-10T21:50:33+00:00Added an answer on May 10, 2026 at 9:50 pm

    Take a look at the following page on working with associations.

    Rails provides you with a bunch of handy methods on your objects.

    Like the following:

    Client.purchases.empty? Client.purchases.size, Client.purchases Client.purchases<<(purchase) Client.purchases.delete(purchase) Client.purchases.find(purchases_id) Client.purchases.find_all(conditions) Client.purchases.build Client.purchases.create 

    When using these methods, you’re taking advantage of the validations on each of the models.

    Hop into your Rails console and create a new client and try any of the above methods. You’ll quickly learn how powerful they are and you’ll be on your way in no time.

    Edit: Here’s a much better guide on Rails associations!

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

Sidebar

Related Questions

I have those two tables: client: id (int) #PK name (varchar) client_category: id (int)
I have two tables: services id client service and clients id name email How
We have two Tables: Document: id, title, document_type_id, showon_id DocumentType: id, name Relationship: DocumentType
I have two tables: Table 1: ID, PersonCode, Name, Table 2: ID, Table1ID, Location,
I have two tables called clients, they are exactly the same but within two
I have two tables, Job and Client. Job has a column, ClientId, that is
I have two tables/entities client and site that have a many to many relationship
I have two tables that are joined together. A has many B Normally you
I have two tables, Book and Tag , and books are tagged using the
I have two tables, both with start time and end time fields. I need

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.