Giter Site home page Giter Site logo

ActiveRecord Multipul Inserts about camping HOT 2 CLOSED

 avatar commented on June 12, 2024
ActiveRecord Multipul Inserts

from camping.

Comments (2)

judofyr avatar judofyr commented on June 12, 2024

(I see that you've written @user.User.create, but I assume that's just a typo and it's actually @user = User.create)

User.create and Company.create always returns the object, even though the validations fail.

I'm not quite sure, but it seems to me that you need a transaction here:

def post
  Company.transaction do
    @company = Company.create!(
      :name => @input.name,
      :sub_domain => @input.subdomain)

    @user = User.create!(
      :company_id => @company.id,
      :first_name => @input.first_name,
      :last_name => @input.last_name,
      :email => @input.email,
      :password => @input.password)
  end
rescue
  @errors = [@company, @user].compact.map(&:full_messages).flatten
  render :errors
else
  redirect Login
end

create! will raise an error if there's a validation error. The transaction block works such that if an error is raised inside it, it will rollback any changes. So if a validation failed on either @Company or @user, none of them will actually be saved. If you don't have the transaction there, you might end up with one Company being created, which is not connected to any User.

Because the rescue captures any error, it's a possibility that @Company or @user is nil, so [@company, @user].compact removes those nils.

The else-block isn't much used in Ruby, but I like it: If no exception is raised, then the else block is called. In this case: If all the validations passes and the data is saved to the DB, we redirect to Login.

There's probably other ways to solve this, so I've also written a message to the rubyonrails-talk mailing-list (the message is currently being moderated) and the Camping mailing-list, and I'll get back to you if someone comes up with a better solution.

from camping.

 avatar commented on June 12, 2024

I got it to work. Turns out it was a lack of ActiveRecord knowledge and not am issue with camping as I expected. Your examples helped and after reviewing the active record docs I see what I was doing wrong. Thanks for the help!

from camping.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.