<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><div dir="ltr"><div><blockquote type="cite"><div dir="ltr"><div>Also presumably you should have</div><div><br></div><div>validates_presence_of :code</div></div></blockquote></div><div><div dir="ltr"><div><br></div></div></div><div>And possibly:</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>validates_uniqueness_of :code, :scope => :a_id</div><div><br></div><div>I'm guessing that would fail the save in code, rather than deadlocking in the DB, and which would then let you handle the error in code and trying another save.</div><div><br></div><div>Srushti</div><div><a href="http://c42.in">http://c42.in</a></div></div></div><div>
</div>
<br><div><div>On 19-Apr-2013, at 2:40 PM, Jonathon Horsman wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div dir="ltr">Hi Andy<div><br></div><div style="">Any reason not to use</div><div style=""><br></div><div style="">before_validation :set_code</div><div style=""><br></div><div style="">rather than</div><div style=""><br></div><div style="">after_create :set_code</div>
<div style=""><br></div><div style="">and set_code method merely sets the value on the object rather than hit the database a second time?</div><div style=""><br></div><div style="">Also presumably you should have</div><div style=""><br>
</div><div style="">validates_presence_of :code</div><div style=""><br></div><div style="">Regards</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On 19 April 2013 09:51, Andrew Stewart <span dir="ltr"><<a href="mailto:boss@airbladesoftware.com" target="_blank">boss@airbladesoftware.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello El Rug,<br>
<br>
Today's question sounds simple – and probably is – but I seem to have answered it wrongly in my code.<br>
<br>
I have two classes A and B, where A has many Bs and B belongs to A.<br>
<br>
B has a `code` method which returns a candidate key (I think that's the correct term), i.e. a unique identifier. It has some domain-meaningful text and a number. The number is what distinguishes the codes of two Bs belonging to the same A. Once a B's code is set at creation it must not change.<br>
<br>
My current implementation looks like:<br>
<br>
class A < ActiveRecord::Base<br>
has_many :bs<br>
<br>
def next_number<br>
increment! :number<br>
number<br>
end<br>
end<br>
<br>
class B < ActiveRecord::Base<br>
belongs_to :a<br>
after_create :set_code<br>
<br>
private<br>
<br>
def set_code<br>
update_attributes code: "#{<a href="http://a.name/" target="_blank">a.name</a>}-#{'%03d' % a.next_number}"<br>
end<br>
end<br>
<br>
This has been working when adding Bs individually but recently I introduced bulk import of Bs from spreadsheets. During imports in production I've begun to see MySQL deadlocks such as:<br>
<br>
#<ActiveRecord::StatementInvalid: Mysql2::Error: Deadlock found when trying to get lock; try restarting transaction: UPDATE `as` SET `number` = 193, `updated_at` = '2013-04-19 07:40:29' WHERE `as`.`id` = 3025><br>
<br>
There must be a better way to do this...?<br>
<br>
Thanks in advance,<br>
<br>
Andy Stewart<br>
<br>
<br>
_______________________________________________<br>
Chat mailing list<br>
<a href="mailto:Chat@lists.lrug.org">Chat@lists.lrug.org</a><br>
<a href="http://lists.lrug.org/listinfo.cgi/chat-lrug.org" target="_blank">http://lists.lrug.org/listinfo.cgi/chat-lrug.org</a><br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br>Jonathon Horsman<br>Director<div>Arctic Kiwi<br>Mobile: 079 42366038<br>Web: <a style="color:rgb(0,153,0)" href="http://www.arctickiwi.com/" target="_blank">http://www.arctickiwi.com</a><div>
Twitter: <a style="color:rgb(0,153,0)" href="http://www.twitter.com/jhorsman" target="_blank">@jhorsman</a><br><img src="http://www.arctickiwi.com/images/arctic-kiwi-logo-email.gif"><br></div></div>
</div>
_______________________________________________<br>Chat mailing list<br><a href="mailto:Chat@lists.lrug.org">Chat@lists.lrug.org</a><br>http://lists.lrug.org/listinfo.cgi/chat-lrug.org<br></blockquote></div><br></body></html>