Iterating through array of Models in rails
Yet another ruby question but this is a bunch of questions in one. I'm really starting to like rails but there are some questions that I'd just like to ask straight out.
Right now, I'm implementing a queue in sqlite. I already 开发者_开发百科have a scaffold setup with this working OK. The purpose is for a web crawler to read through the array and determine which links he should crawl next.
The architecture in the program is 2 controllers. one for Job and one for crawler. The Jobs has the standard Crud interface supplied by scaffold. Where I'm falling down is I'm still trying to understand how these things communicate with eachother.
The Job is formatted as a url:string and depth:decimal. The table is already populated with about 4 objects.
@sitesToCrawl = Job.all
@sitesToCrawl.each {|x|puts Job.url}
I have a bunch of questions about the above.
At the moment, this was supposed to display all the jobs and I foolishly thought it would display plain text but its actually a hexidecimal pointer to the object itself. What Im trying to do is iterate through the @sitesToCrawl and put out each Jobs url.
Questions start here:
1: I know ruby is dynamically typed. Will @sitesToCrawl become an array like i want it to be with each slot containing a job.
2: @sitesToCrawl.each is pretty straighforward and I'm assuming its an iterator.
is X the name od the method or what is the purpose of the symbol or string between |*|3: Puts and print are more or less the same yes? if i say @x = puts 3 then would x be 3?
4: Job.url. Can objects be referenced this way or should I be using
#@sitesToCrawl = db.execute("SELECT url FROM jobs;")
where db is a new database
As Rubish Gupta pointed out, in your block, you should do x.url
, otherwise you're trying to access the url
method on the class Job, not on instances of Job. In other words, in blocks, the items in the pipes are the arguments of the block, and each
will iterate through your array, passing in one item at a time to your block. Check out the doc here.
Just to extend this idea, each
on Hash
es (associative arrays, maps, whatever you know them as) will pass two variables to your block: a key and a value, like this:
a_hash.each {|key_var, val_var| puts "#{key_var} is associated with #{val_var}"}
Also, it's been a bit since I've done plain ActiveRecord models, but you might look into doing
@sitesToCrawl = Job.all.to_a
since Job.all is a lazy finder in that it's building a query in potentia: you've essentially built a query string saying SELECT * FROM jobs
, but it might not be executed until you try to access the items. each
might do that, I can't remember off the top of my head, but if you're using a debugger to look at it, I know you need to_a
to get it to run the query.
You should absolutely be using job_instance.url
- that's the beauty of ActiveRecord, it makes database access easy, provided everything gets set up right :)
Finally, puts and print are almost the same - the difference is that puts "string"
is essentialy print "sting"; STDOUT.flush
- it flushes at the end of the statement.
精彩评论