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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:40:31+00:00 2026-06-08T16:40:31+00:00

First, some background: I have a Company model, a Project model and a Task

  • 0

First, some background:

I have a Company model, a Project model and a Task model. A Project belongs to a company and a Task belongs_to a Project.

The Project model holds several attributes: company_id, date. These attributes uniquely identify a project

I am letting the users create a task by API by POSTing to a URL that contains the details necessary to identify the Project. For example:

POST /projects/<comnpany_name>/<date>/tasks/

In order to make life easier for the users, in case there is no project with the given details, I’d like to create the project on the fly by the given details, and then to create the task and assign it to the project.

…And my problem is:

When there is a problem to create the project, let’s say that the company name is not valid, what is the right way to return the error message and communicate to the user?

I’ll explain what I mean: I added a create_by_name_and_company_name method to the Project:

  def self.create_by_name_and_company_name(name, company_name)
    if company = Company.find_by_name(company_name)
      project = Project.create(company_id: company.id,
                               name: name)      
    else # cannot create this project, trying to communicate the error
      project = Project.new(name: name)
      project.errors.add(:company, 'must have a valid name')
    end
    company
  end

I was hoping that by returning an unsaved Company object, with errors set, will be a good way communicate the error (This is similar to how rails work when there’s a validation error).

The problem is that when calling valid? on the company object, it removed the error I wrote there and adds the regular validation errors (in this case, company can’t be blank).

And a bonus question…

And there is a conceptual problem as well: since I’m creating a model by providing parameters that are being used to create the actual attributes, they doesn’t always map nicely to the errors[:attr] hash. In this case it is not so bad and I’m using the company field for the company name parameter, but I guess this can get messier when the parameters provided to the create method are less similar to the model attributes.

So what is the preferred approach to tackle that problem? Is there something basically wrong with that approach? if so, what is the preferred approach?

  • 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-08T16:40:32+00:00Added an answer on June 8, 2026 at 4:40 pm

    I figure that it is best to avoid such nesting and stick to a shallower API.

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

Sidebar

Related Questions

First some background: The company I work for have decided to create an iPhone
Hi and thanks for your attention. First some background on the question: I have
Some setup background first: I have a cronjob which runs a PHP file called
First some background. I have a batch-type java process run from a DOS batch
First some background: I already have a Result class that I've implemented to carry
First some brief background: I have an existing ASP.NET MVC 1 application using Entity
First some background information: We have three environments for our EJB3 application: test, development
First some background on the questions. I have never used ravedb before and I'm
First, some background info: I am making a compiler for a school project. It
This is a two part question, but first some background information: I have a

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.