Hi! This is my first post in this forum. I'm learning RoR for two weeks and I'm very interested about how to improve this framework.
I was testing one app I'm working in and I had some problems with destroying.
The code is simple (and maybe wrong, as I said i'm just learning!). When you destroy a league, you send a message to all the users associated to this league.
def destroy name = @league.name participants = @league.participants if @league.destroy for participant in participants @message = Message.new(:title => "blabla",:text => "blabla", :receiver_id => participant.id, :user_id => @user.id) @message.save(false) end flash[:notice] = "League '#{name}' was deleted, you are a criminal!" redirect_to route('/leagues') else flash[:error] = 'Something went wrong creating this league!' redirect_to route('/leagues') end end
class League < ActiveRecord::Base has_many :invitations, :dependent => :destroy has_many :participants,:through => :invitations,:source => :user,:conditions => "accepted == 't'" end
First of all: Leagues and invitations are dependent as you can see in the model. When I destroy a League the SQL generated is this: [DEBUG] Invitation Load SELECT * FROM invitations WHERE (invitations.league_id = 1) [DEBUG] Invitation Destroy DELETE FROM invitations WHERE "id" = 12e [DEBUG] Invitation Destroy DELETE FROM invitations WHERE "id" = 15
If you have 100 invitations, you have to do 101 db calls? I think it will be better to just make a DELETE FROM invitations WHERE league_id = 1
Maybe i'm not using :dependent =>:destroy in the correct way...
The second problem i have is about creating messages. When i call to league.destroy it deletes all invitations and then my participants variable is empty. Do you know what can i do?
Thank's in advance and sorry for my english! I'm trying to learn it as well.