[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Check if value has already been used

Chris Gallagher

12/19/2007 3:09:00 PM

Hi,

Im currently working on an issue im having with a ruby method within my
rails project.

i have this method:

def saveAssetTags(asset, taglist)
#add new tags

tagList = taglist.split(/\s*\,\s*/)
#drop all current tags
asset.tags.clear
for metadata in tagList
tag = Tag.find_by_Tag(metadata)
unless tag
tag = Tag.new
tag.Tag = metadata
tag.save
end
asset.tags << tag
end

end

its taking in a list of tags from the client seperated by comma's and
putting them into an array before saving them in my database. this all
works fine unless a user enters the same tag twice in which case i get a
duplication error in the db.

whats the best way to check the array for repeating attributes or to
check as i iterate through the loop?

Cheers,

Chris
--
Posted via http://www.ruby-....

7 Answers

Stefano Crocco

12/19/2007 3:24:00 PM

0

Alle mercoled=C3=AC 19 dicembre 2007, Chris Gallagher ha scritto:
> Hi,
>
> Im currently working on an issue im having with a ruby method within my
> rails project.
>
> i have this method:
>
> def saveAssetTags(asset, taglist)
> #add new tags
>
> tagList =3D taglist.split(/\s*\,\s*/)
> #drop all current tags
> asset.tags.clear
> for metadata in tagList
> tag =3D Tag.find_by_Tag(metadata)
> unless tag
> tag =3D Tag.new
> tag.Tag =3D metadata
> tag.save
> end
> asset.tags << tag
> end
>
> end
>
> its taking in a list of tags from the client seperated by comma's and
> putting them into an array before saving them in my database. this all
> works fine unless a user enters the same tag twice in which case i get a
> duplication error in the db.
>
> whats the best way to check the array for repeating attributes or to
> check as i iterate through the loop?
>
> Cheers,
>
> Chris

I can think of two approaches. If you don't want to take any special action=
if=20
there are duplicates tags (such as report an error), you can remove the=20
duplicates from the array using the Array#uniq method:

tagList =3D taglist.split(/\s*\,\s*/).uniq

The other possibility is to store the found tags in a hash or array. Someth=
ing=20
like:

found_tags =3D {}
for metadata in tagList
if found_tags.has_key? metadata
#do what you need to do for a duplicate tag.
else
found_tags[metadata] =3D true
#do what you need to do for a non-duplicate tag
end
end

I hope this helps

Stefano

Chris Gallagher

12/19/2007 3:25:00 PM

0

not to worry, found the answer myself by thinking about it.

just added .uniq to tagList :0

Thanks,

Chris
--
Posted via http://www.ruby-....

Chris Gallagher

12/19/2007 3:28:00 PM

0

thanks stefano :) good to know I was on the right track!
--
Posted via http://www.ruby-....

Another Unemployed, Lazy American Rightist

9/30/2011 11:55:00 PM

0

Phlip <phlip2...@gmail.com> wrote:

>
> Don't ask about the actual mechanism that Public Option would force
> private insurers out of business, because you won't hear it.
>
> (Ahem. USPS forcing FexEx & UPS out of business lately? Didn't think
> so...)

Socialist Canada's postal system makes money, so does their mint.
Their health care system does a better job at 60% of the USA's cost.

One thing that Americans don't know how to do, no matter who is in
charge, is make government work.

That includes the rightist fanatics.

Hell, the USA can't even make the private sector work. Imbeciles
blaming the government when their private insurance company gouges
them!

When the oil companies and speculators on Wall St. fuck them up the
ass with higher gas prices, they blame the government!


That's useful idiots for you!

Not Sure

10/1/2011 12:45:00 AM

0

On Sep 30, 4:48 pm, Phlip <phlip2...@gmail.com> wrote:
> On Sep 30, 4:19 pm, Christopher  Helms <chrishel...@gmail.com> wrote:
>
> > On Sep 27, 12:46 pm, "Eddie Haskell" <r...@wqpm.com> wrote:
>
> > > All according to plan. Soon the public will be clamoring for the public
> > > option that will put private insurance out of business and total government
> > > control of your life will be achieved.
>
> > > Congrats.
>
> > Thank you. Now fuck off.
>
> Don't ask about the actual mechanism that Public Option would force
> private insurers out of business, because you won't hear it.
>
> (Ahem. USPS forcing FexEx & UPS out of business lately? Didn't think
> so...)

Hiding behind Christopher Helms' skirt, are you? Continue panicking,
union whore :)

Sid9

10/1/2011 1:18:00 AM

0


"Christopher Helms" <chrishelms2@gmail.com> wrote in message
news:5b506654-43de-4e45-a46e-42e9c519ba17@20g2000yqq.googlegroups.com...
> On Sep 27, 12:46 pm, "Eddie Haskell" <r...@wqpm.com> wrote:
>> All according to plan. Soon the public will be clamoring for the public
>> option that will put private insurance out of business and total
>> government
>> control of your life will be achieved.
>>
>> Congrats.
>
>
> Thank you. Now fuck off.

That was Limbo's theme today.
Friday was "Deception Day on Obamacare"


Old Baye

10/1/2011 1:37:00 AM

0

On 9/30/2011 4:48 PM, Phlip wrote:
> On Sep 30, 4:19 pm, Christopher Helms<chrishel...@gmail.com> wrote:
>> On Sep 27, 12:46 pm, "Eddie Haskell"<r...@wqpm.com> wrote:
>>
>>> All according to plan. Soon the public will be clamoring for the public
>>> option that will put private insurance out of business and total government
>>> control of your life will be achieved.
>>
>>> Congrats.
>>
>> Thank you. Now fuck off.
>
> Don't ask about the actual mechanism that Public Option would force
> private insurers out of business, because you won't hear it.
>
> (Ahem. USPS forcing FexEx& UPS out of business lately? Didn't think
> so...)

You see them in residential 6 day mass letter delivery or bulk mail?

Didn't think so...