1

I am working in an Rails app, i am aware of :dependent => delete_all functionality in a Model.

Consider the below example

class TableA < ActiveRecord::Base 

    has_many :tablebs, :dependent => :delete_all

end


class TableB < ActiveRecord::Base 

    belongs_to :tablea

end

If there is corresponding foreign key reference in TableB which represents TableA, then deleting record in TableA using Active record will delete all the corresponding rows in TableB.

But when there is no corresponding values in TableB, it shows like

uninitialized constant TableA::TableB

What i am trying to achieve is, the Active Record should delete dependent values if its present and ignore the step if there is no dependent values.

Is this possible?

thanks, Balan

balanv
  • 10,686
  • 27
  • 91
  • 137

1 Answers1

0

I think the problem is the fact that belongs_to must be related to a table in the singular. So, change the tableas for tablea as below:

I also suggest you to look on this link to see the difference between :delete_all and :destroy.

class TableA < ActiveRecord::Base 
    has_many :tablebs, :dependent => :delete_all
end

class TableB < ActiveRecord::Base 
    belongs_to :tablea
end
Community
  • 1
  • 1
gabrielhilal
  • 10,660
  • 6
  • 54
  • 81
  • actually i am using the table name in singular format.. sorry i missed it in the question.. updating it now.. – balanv Jul 12 '12 at 10:38
  • also `deleteall`should be `delete_all` – gabrielhilal Jul 12 '12 at 10:39
  • Yeah i updated it.. actually i was posting a sample. But in my actual example it was delete_all, and belongs_to entity in singular as you have mentioned – balanv Jul 12 '12 at 10:42
  • have you included the column tablea_id in the tableb? Please include your migration files in your question... it will help to understand the problem.. – gabrielhilal Jul 12 '12 at 10:53