Message from discussion
Using Struct with find_or_create
Date: Thu, 24 May 2012 15:55:02 -0700 (PDT)
From: armanx <arm...@gmail.com>
To: sequel-talk@googlegroups.com
Message-Id: <e5bdf643-c4ab-49d3-9230-b9de534b663c@googlegroups.com>
In-Reply-To: <31f7e7fe-3566-45ca-85ab-2bdc6ef395bd@googlegroups.com>
References: <cb6f7b44-c8e2-4fef-8e1c-6bcd9263696c@googlegroups.com>
<02928b16-521a-49c6-ba0c-10bcf298433f@googlegroups.com>
<31f7e7fe-3566-45ca-85ab-2bdc6ef395bd@googlegroups.com>
Subject: Re: Using Struct with find_or_create
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_29_5397481.1337900102091"
------=_Part_29_5397481.1337900102091
Content-Type: multipart/alternative;
boundary="----=_Part_30_17836664.1337900102091"
------=_Part_30_17836664.1337900102091
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
rohit,
In your gist example, you're passing a hash to find_or_create without
specifying a condition. In that case, which field does Sequel use to
perform the find query? In other words, if you're not defining which column
to use for find, what does Sequel default to? The primary key? Does it use
ALL columns?
On Wednesday, May 23, 2012 1:45:41 AM UTC-7, armanx wrote:
>
> rohit,
>
> Thanks for the reply. I ended up using the following (via
> http://stackoverflow.com/questions/8082423/returning-struct-data-as-a-hash-in-ruby
> ):
>
> user_hash = Hash[user_struct.each_pair.to_a]
>
>
>
>
> On Saturday, May 19, 2012 8:13:54 AM UTC-7, rohit wrote:
>>
>> On Saturday, May 19, 2012 2:12:56 AM UTC-5, armanx wrote:
>>>
>>> I read the documentation on `find_or_create` and learned I can use the
>>> following syntax:
>>>
>>> User.find_or_create(:name=>'Jim', :number=>10)
>>>
>>>
>>> and
>>>
>>> User.find_or_create(:name=>'Jim'){|a| a.number = 10}
>>>
>>>
>>> However, I'd like to pass a standard Ruby struct object, along the lines
>>> of
>>>
>>> User.find_or_create(:id => user_struct.id) { user_struct }
>>>
>>>
>>> Is there a way to achieve this? Maybe not a Sequel-specific question,
>>> but I would appreciate any pointers towards the right direction.
>>>
>>
>> Unfortunately Struct does not have a method that returns its contents as
>> a hash. You could so something like this https://gist.github.com/2731175.
>> I'd re-open Struct and add a to_hash method.
>>
>
------=_Part_30_17836664.1337900102091
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable
rohit,<div><br></div><div>In your gist example, you're passing a hash to fi=
nd_or_create without specifying a condition. In that case, which field does=
Sequel use to perform the find query? In other words, if you're not defini=
ng which column to use for find, what does Sequel default to? The primary k=
ey? Does it use ALL columns?<br><br>On Wednesday, May 23, 2012 1:45:41 AM U=
TC-7, armanx wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;mar=
gin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">rohit,<div>=
<br></div><div>Thanks for the reply. I ended up using the following (via <a=
href=3D"http://stackoverflow.com/questions/8082423/returning-struct-data-a=
s-a-hash-in-ruby" target=3D"_blank">http://stackoverflow.com/<wbr>questions=
/8082423/returning-<wbr>struct-data-as-a-hash-in-ruby</a>)<wbr>:</div><div>=
<br></div><div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0p=
x 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-lef=
t-style:solid;padding-left:1ex">user_hash =3D Hash[user_struct.each_pair.to=
_<wbr>a]</blockquote><div><br></div><div><br></div><br>On Saturday, May 19,=
2012 8:13:54 AM UTC-7, rohit wrote:<blockquote class=3D"gmail_quote" style=
=3D"margin:0;margin-left:0.8ex;border-left:1px #ccc solid;padding-left:1ex"=
>On Saturday, May 19, 2012 2:12:56 AM UTC-5, armanx wrote:<blockquote class=
=3D"gmail_quote" style=3D"margin:0;margin-left:0.8ex;border-left:1px #ccc s=
olid;padding-left:1ex">I read the documentation on `find_or_create` and lea=
rned I can use the following syntax:<div><br></div><blockquote class=3D"gma=
il_quote" style=3D"margin-top:0px;margin-right:0px;margin-bottom:0px;margin=
-left:0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border=
-left-style:solid;padding-left:1ex">User.find_or_create(:name=3D>'<wbr>J=
im', :number=3D>10)</blockquote><div><br></div><div>and</div><div><br></=
div><blockquote class=3D"gmail_quote" style=3D"margin-top:0px;margin-right:=
0px;margin-bottom:0px;margin-left:0.8ex;border-left-width:1px;border-left-c=
olor:rgb(204,204,204);border-left-style:solid;padding-left:1ex">User.find_o=
r_create(:name=3D>'<wbr>Jim'){|a| a.number =3D 10}</blockquote><div=
><br></div><div>However, I'd like to pass a standard Ruby struct object, al=
ong the lines of</div><div><br></div><blockquote class=3D"gmail_quote" styl=
e=3D"margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0.8ex;bo=
rder-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:so=
lid;padding-left:1ex">User.find_or_create(:id =3D> <a href=3D"http:=
//user_struct.id" target=3D"_blank">user_struct.id</a>) { user_struct }</bl=
ockquote><div><br></div><div>Is there a way to achieve this? Maybe not a Se=
quel-specific question, but I would appreciate any pointers towards the rig=
ht direction.</div></blockquote><div><br></div><div>Unfortunately Struct do=
es not have a method that returns its contents as a hash. You could so some=
thing like this <a href=3D"https://gist.github.com/2731175" target=3D"=
_blank">https://gist.github.com/<wbr>2731175</a>. I'd re-open Struct and ad=
d a to_hash method. </div></blockquote></div></blockquote></div>
------=_Part_30_17836664.1337900102091--
------=_Part_29_5397481.1337900102091--