sql - Concurency between 2 where() clause with rails? -


i have in seed.rb :

c1 = chapter.where(number: 1).first_or_create a1 = article.where(number: 1, text: 'foo bar' chapter: c1).first_or_create a2 = article.where(number: 2, text: 'foo bar baz', chapter: c1).first_or_create 

and in article class :

class article    belongs_to :chapter    before_create     self.foo = true if article.where(chapter_id: chapter_id).count == 0       # tried version too, same problem : self.foo = true if chapter.articles.count == 0    end end 

the query excepted count :

select count(*) articles `articles`.`chapter_id` = 1 

but, sql log, have :

for first article :

select count(*) articles `articles`.`number` = 1 , `articles`.`text` = "foo bar" , `articles`.`chapter_id` = 1 , (`articles`.`chapter_id` = 1) 

and second article :

select count(*) articles `articles`.`number` = 2 , `articles`.`text` = "foo bar baz" , `articles`.`chapter_id` = 1 , (`articles`.`chapter_id` = 1) 

my problem before_createis call before create, in seed.rb alwais in cache , combinate in query count...

so count alwais equals 0, foo property alwais set @ true articles

how can independant query?


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -