[colug-432] Ruby ActiveRecord joined tables

Rick Hornsby richardjhornsby at gmail.com
Thu Aug 13 11:43:31 EDT 2015


> On Aug 13, 2015, at 08:05, Rob Funk <rfunk at funknet.net> wrote:
> 
> Rick Hornsby wrote:
>> I'm trying to write a Ruby script (not RoR) using ActiveRecord.  The
>> documentation is confusing to say the least, probably because while I
>> know what it is, I've never worked with ActiveRecord before.
> 
> Yikes. Most ActiveRecord documentation is oriented toward use inside
> Rails (or maybe vice-versa), so you're not going the easy way here.

This helps - thanks.  I don't feel quite as stupid now.  I went toward ActiveRecord thinking that's what I should be using.  While so many hits for "Ruby mysql" say to use the mysql gem, the author says that the gem is deprecated[1] so I went hunting for another solution.


>> I’m having a hard time finding somewhere that shows both the has_one
>> syntax and how to actually use that to retrieve records - because
>> other places show using a Class.join syntax - but not how or if it
>> relates to the has_one syntax in the class?
>> 
>> Sorry - very confused. I know I'm missing something, but I'm not
>> sure what?
> 
> I think what you're missing is that ActiveRecord is a bad fit for your
> task.

Yes. I agree with your assessment.

> I wonder if Sequel would be a better fit, since it's designed for use
> outside Rails, though it may have some of the same naming issues as
> ActiveRecord.  http://twin.github.io/ode-to-sequel/

I could do all of this in Perl with DBI, but I'm forcing myself to learn/practice Ruby and I'm already using the super easy vacuum gem to fetch from the Amazon API.

I hadn't seen the mysql2 gem[2] until I went to go back to find the link for the mysql gem[1]. mysql2 appears to be maintained. That should work better.  I'm not trying to do anything crazy that demands I use ActiveRecord.  AR seems very interesting to learn, but not an appropriate tool for this.

Background- SONGLIST comes from the database behind SpacialAudio. The SONGLIST table has some bad track metadata - especially the genre.  Most genres are missing, and many are wrong.  For about 75% of the ~20,000 tracks, I have a UPC.  I need to call the Amazon product API, and store the associated JSON response. I can then process the JSON response offline (i.e. to get the genre) without having to call the Amazon API 16,000 * N times because I didn't account for something.  I looked at other APIs including FreeDB and Gracenote - none of them appear to be capable of  processing a request by a unique identifier I have available i.e. UPC. One might take the hex disc ID, but I don't have that data.

I think a standard mysql interface will be fine for this, but I'll keep Sequel in mind.

thanks again!
-rick

[1] https://rubygems.org/gems/mysql/versions/2.9.1 <https://rubygems.org/gems/mysql/versions/2.9.1>
[2] https://rubygems.org/gems/mysql2/versions/0.3.18

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.colug.net/pipermail/colug-432/attachments/20150813/8ed414e2/attachment.html 


More information about the colug-432 mailing list