railsadminteam / rails_admin

RailsAdmin is a Rails engine that provides an easy-to-use interface for managing your data
MIT License
7.9k stars 2.26k forks source link

HABTM Validation Error doesn't delete join table record #1921

Open richardpeng opened 10 years ago

richardpeng commented 10 years ago

I have the following self-referencing association:

has_and_belongs_to_many :parts, class_name: Asset, join_table: :stocks, foreign_key: :asset_id, association_foreign_key: :part_id

and this validation:

 validate :stock_check

  def stock_check
    parts.each do |part|
      if Stock.where(part_id: part.id).size > part[:stock]
        errors.add(:base, 'Not enough stock to assign additional assets.')
      end
    end
  end

When the validation fails, the record is left in the join table. Looking at the SQL queries, the join table records are created in a separate transaction from the ones for the main record and so isn't rolled back when the validation fails.

3den commented 10 years ago

I am not being able to delete HABTM records even without validation

Showing /usr/local/var/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/rails_admin-0.6.2/app/views/rails_admin/main/_delete_notice.html.haml where line #11 raised:

undefined method `categories_vendors' for #<Category:0x007fa04a0c58e0>
Extracted source (around line #11):
     - else
       = wording
     %ul
       - @abstract_model.each_associated_children(object) do |association, child|
         %li
           - child_config = RailsAdmin.config(child)
           = @abstract_model.model.human_attribute_name association.name

Trace of template inclusion: /usr/local/var/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/rails_admin-0.6.2/app/views/rails_admin/main/delete.html.haml
superacidjax commented 10 years ago

I'm having the same issue.

evanbeard commented 10 years ago

+1

3den commented 10 years ago

updating rails have worked for me

superacidjax commented 10 years ago

Worked for me I discovered that after posting. Thanks! I recommend this issue be closed.