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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:45:42+00:00 2026-05-27T11:45:42+00:00

How would I destroy a Product if it doesn’t belong to a Store after_save

  • 0

How would I destroy a Product if it doesn’t belong to a Store after_save?

class Product < ActiveRecord::Base
  attr_accessible :price, :name, :product_store
  belongs_to :store
  attr_reader :product_store

  # I need this to do also "p.product_store.blank?"
  after_save { |p| p.destroy if p.name.blank? && p.price.blank? }

  def product_store=(id) # using Jquery TokenInput so only needed this.
    self.store_id = id
  end
end

I’ve tried a couple of different approaches like:

after_save { |p| p.destroy if p.name.blank? && p.price.blank? && p.product_store.blank? }
after_save { |p| p.destroy if p.name.blank? && p.price.blank? && p.store.id.blank? }
after_save { |p| p.destroy if p.name.blank? && p.price.blank? && p.store_id.blank? }
after_save { |p| p.destroy if p.name.blank? && p.price.blank? && p.store.blank? }

But these didn’t work so I ask for your help on how it would be done?


Here is my form and controller: https://gist.github.com/1472629

  • 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-27T11:45:43+00:00Added an answer on May 27, 2026 at 11:45 am

    how about use ActiveModel::Validations?
    you don’t want to save record if some attribute not filled.so you need validation.

    class Product < ActiveRecord::Base
      validates_presence_of :name, :price, :store_id
    end
    

    Edit:

    your code looks want to create muti products use common attributes.
    may be params like this:

    {"products" => {:1 => {:name => "good", :desc => "sss"}}, {:2 => {:name => "good", :desc => "tt"}}, "product" => {:price => "10"} }
    

    controller
    def create_multiple
    @products = params[:products].values.collect do |up|
    Product.new(up.merge(params[:product]))
    end
    if @products.each(&:save)
    redirect_to :back, :notice => “Success!”
    else
    render :new
    end
    end

    Validation do not need change. but the form will a bit complicate.

    form_tag "/products" do
      %p
       = text_field_tag "product[price]", @products.first.price
       = @products.first.errors[:price]
    
      - @products.each_with_index do |product, idx|
         = text_field_tag "products[#{idx}][name]", product.name
         = product.errors[:name]
    
      = submit_tag :submit
    

    the code is simple, you can enhance the view yourself.

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

Sidebar

Related Questions

I have the following models: class Release < ActiveRecord::Base has_many :products, :dependent => :destroy
I have a the following models: class Release < ActiveRecord::Base has_many :products, :dependent =>
Would it be possible to write a class that is virtually indistinguishable from an
assume the following class is given: class Base{ public: Base() {} Base( const Base&
I would like to build a model where a class of ServiceRegions has a
I would appreciate your help on this: I have 2 models: class Author <
Would it not make sense to support a set of languages (Java, Python, Ruby,
Would it be possible to print Hello twice using single condition ? if condition
Would it be possible to show an image in full screen mode using silverlight.
Would the following SQL remove also the index - or does it have to

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.