After_save does not work consecutively

Rails 3.1.3

Related to my old thread

http://www.ruby-forum.com/topic/3802905#new

video.rb has

after_save :create_first_script
private
def create_first_script
@script = Script.new(:video_id => self.id, :startp => 0, :text =>
'ToDo: ')
@script.save
end

The problem is that after destroying all data, a freshly created video
DOES create the first Script, but the following videos DO NOT.

I have added User table so that Video and Script classes are only
editable for Users that own them.
The association is

User:1 – n:Video:1–n:Script

To be honest, I have no idea which part of my code is causing this
problem. I am sure the information provided here is not enough. I
appreciate if you point out what I need to show you in order to gain
some help.

Thanks in advance.

soichi

Do know why fresh created video can create script but following video
does not. What i am 100% sure is that after_save is not right place to
put this logic. Cause any object’s update will invoke after save, so new
script will created every update.


rubyonrailsx
Sent with Sparrow (http://www.sparrowmailapp.com/?sig)

Looking at the log…
The first one is successful in creating the first Script, the second
one is not.
SQL is not working for the second but certainly after_save method is
called…(‘first script action HERE!!!’ is logger.info).

Does it help?

soichi

Started POST “/videos” for 127.0.0.1 at 2012-03-16 14:08:42 +0900
Processing by VideosController#create as HTML
Parameters: {“utf8”=>“✓”,
“authenticity_token”=>"/AYk7ocjjteBKH8fDpQZ7FKzme/0nSsT2kalq8FgI18=",
“video”=>{“title”=>“Rush Limbaugh Calls a Female Georgetown Student,
Sandra Fluke, a ‘Slut’”,
“url”=>“http://www.youtube.com/watch?v=3KNpn-XGM04”, “language”=>“1”,
“genre”=>“news”}, “commit”=>“Create Video”}
User Load (0.1ms) SELECT “users”.* FROM “users” WHERE “users”.“id” =
1 LIMIT 1
SQL (0.7ms) INSERT INTO “videos” (“created_at”, “genre”, “language”,
“title”, “updated_at”, “url”, “user_id”) VALUES (?, ?, ?, ?, ?, ?, ?)
[[“created_at”, Fri, 16 Mar 2012 14:08:43 JST +09:00], [“genre”,
“news”], [“language”, “1”], [“title”, “Rush Limbaugh Calls a Female
Georgetown Student, Sandra Fluke, a ‘Slut’”], [“updated_at”, Fri, 16 Mar
2012 14:08:43 JST +09:00], [“url”,
http://www.youtube.com/watch?v=3KNpn-XGM04”], [“user_id”, 1]]
first script action HERE!!!
(0.2ms) SELECT 1 FROM “scripts” WHERE “scripts”.“startp” = 0.0 LIMIT
1
SQL (0.5ms) INSERT INTO “scripts” (“created_at”, “startp”, “text”,
“updated_at”, “video_id”) VALUES (?, ?, ?, ?, ?) [[“created_at”, Fri,
16 Mar 2012 14:08:43 JST +09:00], [“startp”,
#BigDecimal:102b7cd18,‘0.0’,9(36)], [“text”, "ToDo: "], [“updated_at”,
Fri, 16 Mar 2012 14:08:43 JST +09:00], [“video_id”, 11]]
Redirected to http://localhost:3001/videos/11
Completed 302 Found in 211ms

Started POST “/videos” for 127.0.0.1 at 2012-03-16 14:08:59 +0900
Processing by VideosController#create as HTML
Parameters: {“utf8”=>“✓”,
“authenticity_token”=>"/AYk7ocjjteBKH8fDpQZ7FKzme/0nSsT2kalq8FgI18=",
“video”=>{“title”=>“bergman trailer persona”,
“url”=>“http://www.youtube.com/watch?v=XZsU_ACYSAA”, “language”=>“1”,
“genre”=>“persona”}, “commit”=>“Create Video”}
User Load (0.1ms) SELECT “users”.* FROM “users” WHERE “users”.“id” =
1 LIMIT 1
SQL (0.7ms) INSERT INTO “videos” (“created_at”, “genre”, “language”,
“title”, “updated_at”, “url”, “user_id”) VALUES (?, ?, ?, ?, ?, ?, ?)
[[“created_at”, Fri, 16 Mar 2012 14:09:00 JST +09:00], [“genre”,
“persona”], [“language”, “1”], [“title”, “bergman trailer persona”],
[“updated_at”, Fri, 16 Mar 2012 14:09:00 JST +09:00], [“url”,
http://www.youtube.com/watch?v=XZsU_ACYSAA”], [“user_id”, 1]]
first script action HERE!!!
(0.2ms) SELECT 1 FROM “scripts” WHERE “scripts”.“startp” = 0.0 LIMIT
1
Redirected to http://localhost:3001/videos/12
Completed 302 Found in 207ms

did not quite look into it.
But i strongly suspect this

“(0.2ms) SELECT 1 FROM “scripts” WHERE “scripts”.“startp” = 0.0 LIMIT
1”

This will return true/1 in any condition.


rubyonrailsx
Sent with Sparrow (http://www.sparrowmailapp.com/?sig)

I found the mistake in my code.

I have put one of the fields of Script, startp, to become “:uniquness =>
true”.

I didn’t know that it is effective for all sets of Script instances that
belong to a single Video.

Anyway, thanks for your answers.

soichi

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs