ruby - Iterating through array of Models in rails -


yet ruby question bunch of questions in one. i'm starting rails there questions i'd ask straight out.

right now, i'm implementing queue in sqlite. have scaffold setup working ok. purpose web crawler read through array , determine links should crawl next.

the architecture in program 2 controllers. 1 job , 1 crawler. jobs has standard crud interface supplied scaffold. i'm falling down i'm still trying understand how these things communicate eachother.

the job formatted url:string , depth:decimal. table populated 4 objects.

@sitestocrawl = job.all  @sitestocrawl.each {|x|puts job.url} 

i have bunch of questions above.

at moment, supposed display jobs , foolishly thought display plain text hexidecimal pointer object itself. im trying iterate through @sitestocrawl , put out each jobs url.

questions start here:

1: know ruby dynamically typed. @sitestocrawl become array want each slot containing job.

2: @sitestocrawl.each pretty straighforward , i'm assuming iterator.
x name od method or purpose of symbol or string between |*|

3: puts , print more or less same yes? if @x = puts 3 x 3?

4: job.url. can objects referenced way or should using

 #@sitestocrawl = db.execute("select url jobs;") 

where db new database

as rubish gupta pointed out, in block, should x.url, otherwise you're trying access url method on class job, not on instances of job. in other words, in blocks, items in pipes arguments of block, , each iterate through array, passing in 1 item @ time block. check out doc here.

just extend idea, each on hashes (associative arrays, maps, whatever know them as) pass two variables block: key , value, this:

a_hash.each {|key_var, val_var| puts "#{key_var} associated #{val_var}"} 

also, it's been bit since i've done plain activerecord models, might doing

@sitestocrawl = job.all.to_a 

since job.all lazy finder in it's building query in potentia: you've built query string saying select * jobs, might not executed until try access items. each might that, can't remember off top of head, if you're using debugger @ it, know need to_a run query.

you should absolutely using job_instance.url - that's beauty of activerecord, makes database access easy, provided gets set right :)

finally, puts , print almost same - difference puts "string" essentialy print "sting"; stdout.flush - flushes @ end of statement.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -