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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:21:22+00:00 2026-05-28T04:21:22+00:00

I have two associated models: Apartment and Lessor . And I need to be

  • 0

I have two associated models: Apartment and Lessor. And I need to be able to create Lessor from Apartment form.

In Apartment model:

belongs_to :lessor
before_save :save_lessor
...
def lessor_cellphone= val
  @cellphone = val
end
...
private
def save_lessor
  if Lessor.exists? :cellphone => @cellphone
    self.lessor = Lessor.find_by_cellphone @cellphone
  else
    self.create_lessor :cellphone => @cellphone
  end
  @cellphone = nil
end

In Lessor model:

validates :cellphone, :format => {:with => /\d{11}/}, :uniqueness => true
has_many :apartments, :dependent => :nullify

But when I trying to create Apartment with invalid cellphone, Lessor is not created becouse validation fails, but `Apartment is created.

What is the best way to validate cellphone (and maybe more) and rise error in the Apartment form?

  • 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-05-28T04:21:22+00:00Added an answer on May 28, 2026 at 4:21 am

    I think maybe a better solution would be to use accepts_nested_attributes_for to create a nested model through the form of an other.

    See http://railscasts.com/episodes/196-nested-model-form-part-1 or http://asciicasts.com/episodes/196-nested-model-form-part-1 for the text version.

    Nontheless, if you want to use your existing solution:
    If you return false in a before_* callback all the later callbacks and the associated action are cancelled, see http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html

    So I guess it would be something like

    def create_lessor(data)
      # validate data here
      return false if # data not valid
    end
    
    def save_lessor
      rc = true
      if Lessor.exists? :cellphone => @cellphone
        self.lessor = Lessor.find_by_cellphone @cellphone
      else
        rc = self.create_lessor(:cellphone => @cellphone)
      end
      @cellphone = nil
      rc  # return the return code
    end
    

    It’s not a beautiful solution AT ALL, but I think you get the idea…

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

Sidebar

Related Questions

I have two models that are associated. I have an Artist model and a
I have two models that are associated with each other. Customer has_one :primary_contact And
I have two models, Tweets and TweetsLocations, associated to each other with: class Tweets
I have associated two models Businesses and Ratings . A business can be rated
I have two models, associated via an has_and_belongs_to_many association. I want to destroy the
I have two models, associated with a HABTM (actually using has_many :through on both
So I have two models, a Ranking model and a UserRanking model. The app
I have three models: System_Contact System Contact_list The Contact_List model has two fields: contact
I have two models, Room and Image . Image is a generic model that
I have two models: Conversation and Message . A message belongs_to a conversation, and

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.