Rectangle 27 180

You are right. If you want to delete the User and all associated objects -> destroy_all However, if you just want to delete the User without suppressing all associated objects -> delete_all

  • destroy / destroy_all: The associated objects are destroyed alongside this object by calling their destroy method
delete
delete_all

It should also be noted that 1) Callbacks are not called when using delete_all, and 2) destroy_all instantiates all the records and destroys them one at a time, so with a very large dataset, this could be painfully slow.

suppose i'm running a before_destroy method in the model - if i use delete_all then this method won't run? secondly if i employ a before_delete method in my model, will this run when i run delete or delete_all in the rails console?

ruby on rails - delete_all vs destroy_all? - Stack Overflow

ruby-on-rails ruby database activerecord
Rectangle 27 18

delete_all is a single SQL DELETE statement and nothing more. destroy_all calls destroy() on all matching results of :conditions (if you have one) which could be at least NUM_OF_RESULTS SQL statements.

If you have to do something drastic such as destroy_all() on large dataset, I would probably not do it from the app and handle it manually with care. If the dataset is small enough, you wouldn't hurt as much.

ruby on rails - delete_all vs destroy_all? - Stack Overflow

ruby-on-rails ruby database activerecord
Rectangle 27 16

To avoid the fact that destroy_all instantiates all the records and destroys them one at a time, you can use it directly from the model class.

So instead of :

You can do :

u = User.find_by_name('JohnBoy')
UsageIndex.destroy_all "user_id = #{u.id}"

The result is one query to destroy all the associated records

Will it call the destroy callbacks on associated records, or is UsageIndex.destroy_all equivalent with UsageIntex.delete_all ?

ruby on rails - delete_all vs destroy_all? - Stack Overflow

ruby-on-rails ruby database activerecord
Rectangle 27 1

Ive made a small gem that can alleviate the need to manually delete associated records in some circumstances.

This gem adds a new option for ActiveRecord associations:

When you destroy a record, all records that are associated using this option will be deleted recursively (i.e. across models), without instantiating any of them.

Note that, just like dependent: :delete or dependent: :delete_all, this new option does not trigger the around/before/after_destroy callbacks of the dependent records.

However, it is possible to have dependent: :destroy associations anywhere within a chain of models that are otherwise associated with dependent: :delete_recursively. The :destroy option will work normally anywhere up or down the line, instantiating and destroying all relevant records and thus also triggering their callbacks.

This is fantastic! I wonder why not more people have watched/starred/forked it on github.. is it still working well?

@Magne Thanks! It should be working. The tests run on Ruby 2.4.1 and Rails 5.1.1. So far I've only used it privately and not in major production apps, hence the major version "0", but I never noticed any issues. It's also fairly simple, so it should be fine.

Cool. :) I am running a project on Ruby 2.3.1, and 'rails', '~>4.1.14', and am sadly forced to rely on activerecord (~> 4.1.0) due to other gems. I see that delete_recursively is resolved to 0.9.0. Is there an older version of it that would work with activerecord 4.1 ? I couldn't find any in the releases tab on github.

@Magne I found it actually works for activerecord as low as 4.1.14 and have released gem version 1.0.0 with a relaxed dependency. Keep in mind that Rails' 4.1 branch no longer receives security updates, though.

ruby on rails - delete_all vs destroy_all? - Stack Overflow

ruby-on-rails ruby database activerecord
Rectangle 27 121

On a Rails' model association you can specify the :dependent option, which can take one of the following three forms:

  • :destroy/:destroy_all The associated objects are destroyed alongside this object by calling their destroy method
:delete/:delete_all
:destroy
:nullify
NULL
save
:restrict
:delete
:destroy_all

@MikeCampbell, :delete and :destroy_all options do not exist. However, there are class methods on models that are called delete and destroy_all so it might be the reason for confusion.

ruby - Rails :dependent => :destroy VS :dependent => :delete_all - Sta...

ruby-on-rails ruby model associations
Rectangle 27 121

On a Rails' model association you can specify the :dependent option, which can take one of the following three forms:

  • :destroy/:destroy_all The associated objects are destroyed alongside this object by calling their destroy method
:delete/:delete_all
:destroy
:nullify
NULL
save
:restrict
:delete
:destroy_all

@MikeCampbell, :delete and :destroy_all options do not exist. However, there are class methods on models that are called delete and destroy_all so it might be the reason for confusion.

ruby - Rails :dependent => :destroy VS :dependent => :delete_all - Sta...

ruby-on-rails ruby model associations
Rectangle 27 169

The difference is with the callback.

The :delete_all is made directly in your application and deletes by SQL :

DELETE * FROM users where compagny_id = XXXX

With the :destroy, there is an instantiation of all of your children. So, if you can't destroy it or if each has their own :dependent, its callbacks can be called.

The instantiation and the calling of destroy on each of the children objects will be slow if you have a lot of children (and n^2 if you have grandchildren, and so on). delete_all is the kind of "nuke it from orbit" solution where you don't care about / don't have any before / after destroy callbacks on the models.

ruby - Rails :dependent => :destroy VS :dependent => :delete_all - Sta...

ruby-on-rails ruby model associations
Rectangle 27 170

The difference is with the callback.

The :delete_all is made directly in your application and deletes by SQL :

DELETE * FROM users where compagny_id = XXXX

With the :destroy, there is an instantiation of all of your children. So, if you can't destroy it or if each has their own :dependent, its callbacks can be called.

The instantiation and the calling of destroy on each of the children objects will be slow if you have a lot of children (and n^2 if you have grandchildren, and so on). delete_all is the kind of "nuke it from orbit" solution where you don't care about / don't have any before / after destroy callbacks on the models.

ruby - Rails :dependent => :destroy VS :dependent => :delete_all - Sta...

ruby-on-rails ruby model associations
Rectangle 27 26

delete_all can delete multiple data from self table

DELETE * FROM table where field = 'xyz'

Controls what happens to the associated objects when their owner is destroyed. Note that these are implemented as callbacks, and Rails executes callbacks in order. Therefore, other similar callbacks may affect the :dependent behavior, and the :dependent behavior may affect other callbacks.

:destroy

:delete_all causes all the associated objects to be deleted directly from the database (so callbacks will not be executed).

:nullify

:restrict_with_exception causes an exception to be raised if there are any associated records.

:restrict_with_error causes an error to be added to the owner if there are any associated objects.

If using with the :through option, the association on the join model must be a belongs_to, and the records which get deleted are the join records, rather than the associated records.

ruby - Rails :dependent => :destroy VS :dependent => :delete_all - Sta...

ruby-on-rails ruby model associations
Rectangle 27 55

Is Grails now worth it vs Ruby or other roll your own?

Of course, the answer is not 'Yes' or 'No' but it depends. It depends on your requirements (do you need to be in the Java World?), on your preferences as well (do you prefer the domain-oriented development, do you prefer Groovy...)? However, I can answer that Grails is a serious alternative to Rails. I believe that whatever is your Rails application, you can do it with Grails as well. But depending on the nature of your project, it might take more or less time. Again, if you are familiar with Rails but not with Grails, Rails is the safer option.

Yes. If you take a look at my initial messages (in this website or others), I was complaining a lot about Grails bugs. But, you just need to remember that Grails is a little rough on the edge (not too much use of domain inheritance ,for instance) and once you are familiar with the framework, you don't experience too much bad surprises. I am not saying that Grails is not buggy. It is certainly more than Rails. But also, it is more usable than buggy. A piece of advice for that : use as few plugins as possible. Because many of them are buggy and some are not compatible among themselves. So, do not include grails plugin unless you are sure that the grails plugin is up-to-date, non-intrusive and tested (by yourself).

Yes. You almost do not need to deal with DB design. Configuration is almost done for you from the beginning thanks to Convention over Configuration. Your application is easily maintenable. The only drawback I see is front-end development that is not as rich as other technologies (like Rails or ASP)

I cannot say because I still didn't go my website live but I am pretty confident since sky.com is using Grails and the sites attract significant traffic - around 7 million page views per day . Again performance depends a lot on your application architecture and design decisions.

Is the Eclipse plug-in better than it was and fit for purpose?

No idea. I am using IntelliJ but I guess it is not much better than one year ago according to complaining messages I see on the Grails realm.

I second the point about the plugins. You have no guarantee that plugins will be updated for newer versions of Grails. I would recommend that you stick with those backed by Grails / SpringSource.

Plugins are an interesting bunch in the grails community. If you look at plugins as black box that you use with your app, then I think you are going to have problems. But if you look at plugins as templates that the grails community has provided you will get along much better.

NetBeans or IntelliJ are the IDEs of choice for Grails. IntelliJ if you have the money.

Rails for 5 years, Groovy/Grails for 8 months. Familiarity says Rails is better, but Grails is eminently usable once you're over the initial learning hump. Grails feels a little config file happy in comparison (do I really need 6 or 7 for a single plugin module?), but the persistence layer is very nice to work with. As for IDEs, used STS for 4 months with many frustrations, then tried IntelliJ for 4 days, and never looked back.

ruby on rails - Is Grails (now) worth it? - Stack Overflow

ruby-on-rails grails groovy language-comparisons
Rectangle 27 201

If you are looking for a way to it without SQL you should be able to use delete_all.

Post.delete_all

or with a criteria

Post.delete_all "person_id = 5 AND (category = 'Something' OR category = 'Else')"

The records are deleted without loading them first which makes it very fast but will break functionality like counter cache that depends on rails code to be executed upon deletion.

It's worth noting that if you've got associations with :dependent => :destroy, or anything that needs to be cleaned up upon deletion, you'll probably want Post.destroy_all - though it is much slower. See apidock.com/rails/ActiveRecord/Base/destroy_all/class

This answer assumes that the table has a model associated with it. The OP didn't specify this - what if the table is a join table?

@BradWerth whether or not it is considered by some to be good or bad style, I'm just saying it is feasible that a Rails db has tables that aren't ActiveRecord models. The question asks about deleting record from a 'table' and I'm just pointing or the assumption held in the answer.

ruby on rails - Deleting all records in a database table - Stack Overf...

ruby-on-rails ruby database ruby-on-rails-3
Rectangle 27 200

If you are looking for a way to it without SQL you should be able to use delete_all.

Post.delete_all

or with a criteria

Post.delete_all "person_id = 5 AND (category = 'Something' OR category = 'Else')"

The records are deleted without loading them first which makes it very fast but will break functionality like counter cache that depends on rails code to be executed upon deletion.

It's worth noting that if you've got associations with :dependent => :destroy, or anything that needs to be cleaned up upon deletion, you'll probably want Post.destroy_all - though it is much slower. See apidock.com/rails/ActiveRecord/Base/destroy_all/class

This answer assumes that the table has a model associated with it. The OP didn't specify this - what if the table is a join table?

@BradWerth whether or not it is considered by some to be good or bad style, I'm just saying it is feasible that a Rails db has tables that aren't ActiveRecord models. The question asks about deleting record from a 'table' and I'm just pointing or the assumption held in the answer.

ruby on rails - Deleting all records in a database table - Stack Overf...

ruby-on-rails ruby database ruby-on-rails-3
Rectangle 27 26

delete_all can delete multiple data from self table

DELETE * FROM table where field = 'xyz'

Controls what happens to the associated objects when their owner is destroyed. Note that these are implemented as callbacks, and Rails executes callbacks in order. Therefore, other similar callbacks may affect the :dependent behavior, and the :dependent behavior may affect other callbacks.

:destroy

:delete_all causes all the associated objects to be deleted directly from the database (so callbacks will not be executed).

:nullify

:restrict_with_exception causes an exception to be raised if there are any associated records.

:restrict_with_error causes an error to be added to the owner if there are any associated objects.

If using with the :through option, the association on the join model must be a belongs_to, and the records which get deleted are the join records, rather than the associated records.

ruby - Rails :dependent => :destroy VS :dependent => :delete_all - Sta...

ruby-on-rails ruby model associations
Rectangle 27 17

has_many :posts_tags, dependent: :delete_all

is exactly what you want. You do not want to declare this on the has-many-though association :tags, as that will destroy all associated Tags. What you want to delete is the association itself - which is what the PostTag join model represents.

So why do the docs say what they do? You are misunderstanding the scenario that the documentation is describing:

Post.find(1).destroy
Post.find(1).tags.delete

The first call (your scenario) will simply destroy the Post. That is, unless you specify a :dependent strategy, as I suggest you do. The second call is what the documentation is describing. Calling .tags.delete will not (by default) actually destroy the tags (since they are joined by has-many-through), but the associated join model that joins these tags.

ruby on rails - Proper way to delete has_many :through join records? -...

ruby-on-rails ruby-on-rails-3
Rectangle 27 1

Actually the main difference is that any callbacks will not be invoked when :delete_all was used. But when used :destroy the callbacks stack (:after_destroy, :after_commit ...) will be fired.

Consequently, if you have touch:ing declarations in models being deleted then it's better to use dependent: :delete_all rather 'dependent: :destroy'.

ruby - Rails :dependent => :destroy VS :dependent => :delete_all - Sta...

ruby-on-rails ruby model associations
Rectangle 27 1

Actually the main difference is that any callbacks will not be invoked when :delete_all was used. But when used :destroy the callbacks stack (:after_destroy, :after_commit ...) will be fired.

Consequently, if you have touch:ing declarations in models being deleted then it's better to use dependent: :delete_all rather 'dependent: :destroy'.

ruby - Rails :dependent => :destroy VS :dependent => :delete_all - Sta...

ruby-on-rails ruby model associations
Rectangle 27 16

This is the 'Mockist vs Classicist' dilemma addressed in Martin Fowler's Mocks Aren't Stubs. Using mocks (doubles) throughout is necessarily going to require stubbing out other methods on collaborators and exposing the implementation. That is part of the price you pay for the speed and flexibility of mocking.

Another issue is that there is no natural 'subject' for the spec, because this is a class method. You end up with three objects that each need to be updated; in a sense they are alternately subjects and collaborators depending on which expectation is being exercised. You can make this more clear by setting one expectation per example:

describe MealServicer do
  context ".serve_meal" do
    let(:order) { double(:order) }
    let(:meal) { double(:meal) }
    let(:customer) { double(:customer, id: 123, order: order }

    it "updates the meal" do
      allow(OrderServicer).to_receive(:add_meal_to_order)
      allow(CRM).to_receive(:update_customer_record)
      expect(meal).to receive(:update_attributes).with(status: "served", customer_id: 123)
      MealServicer.serve_meal(meal, customer)
    end

    it "adds the meal to the order" do
      allow(meal).to receive(:update_attributes)
      allow(CRM).to_receive(:update_customer_record)
      expect(OrderServicer).to receive(:add_meal_to_order).with(meal, order)
      MealServicer.serve_meal(meal, customer)
    end

    it "updates the customer record" do
      allow(meal).to receive(:update_attributes)
      allow(OrderServicer).to_receive(:add_meal_to_order)
      expect(CRM).to receive(:update_customer_record).with(customer)
      MealServicer.serve_meal(meal, customer)
    end
  end
end

Now the stubs are always the dependencies, and the expectations are the things being tested, which clarifies the intent of the spec.

I disagree. If you separate the expectations, then you can test first and write code to make the tests pass, if you work on one example at a time.

ruby on rails - Rspec advice for testing service objects - Stack Overf...

ruby-on-rails ruby unit-testing rspec
Rectangle 27 31

I use and like both, and the problem with a question like this is that they're so different that you could go on all day about apples vs. oranges, but since your question is pretty non-specific, I'll just say:

If you consider yourself "a .NET guy," then you should learn a language which works very differently than C#, and a dynamic language like Ruby isn't a bad place to start. (Another option would be a functional language like Haskell, Scala, etc.)

You will be a better programmer after you do, and you won't have to call yourself "a .NET guy" anymore!

I agree but my goal in to have a real web site up and running as fast as possible and extending my horizons is not my main goal right now. I sure do value learning new great things like Ruby but my focus right now is to pick the right option for limited time that I have. And why are the apples and oranges where their goal is to be a web development framework?

Getting a real site running quickly will not be a problem with Rails. Do yourself a favor: Spend a week working through this tutorial before you decide it will hurt your time to market. Only risk is that you'll learn something! The reason they're "apples and oranges" is that they work differently, run on different platforms, etc. They have MVC architecture in common, and there's clearly a lot of influence -- mostly in one direction -- but they're very different worlds. It can be a mind-expanding difference.

Great, thank you. I'll do so. Would you please still comment what one could get with Rails which is available in .MVC from practical perspective given that you have used both?

Ruby on Rails vs ASP.NET MVC 3 for a .NET Guy? - Stack Overflow

asp.net ruby-on-rails asp.net-mvc-3
Rectangle 27 13

I agree with jbbarth that it's not as simple as 422 vs 500.

422 is good for validation errors, but you don't want to be catching the db errors also.

if @record.invalid?
  # failure of validations => status 422
elsif @record.save
  # status 200
else
  # failure when saving => status 500
end

ruby on rails - Is HTTP status 422 appropriate for records that fail u...

ruby-on-rails http-status-codes
Rectangle 27 12

Nice one, just got stuck in the exact some place for hour and a half for the exact same reason. Ughh

ruby on rails - Simple Rspec test failing from Michael Hartl RoR Tuoti...

ruby-on-rails rspec capybara