[colug-432] Ruby ActiveRecord joined tables

Rick Hornsby richardjhornsby at gmail.com
Wed Aug 12 22:44:16 EDT 2015


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.

I was given the table SONGLIST, and I created the table amazon_metadata.  The PK on SONGLIST is 'id' and the PK on amazon_metadata is 'songid'.  The tables have a 1-to-1 relationship.  (If it helps explain, I could have just appended the amazon_metadata columns to SONGLIST, but I don't want to modify SONGLIST.)

I set up my ActiveRecord thusly:
	https://gist.github.com/rjhornsby/26addcbaef5459182947

The relationship has_one and belongs_to cannot be enough, because it doesn't describe what fields the tables should be joined on.  Supposing I figure out *where* to write a join clause (that seems to be missing from all the docs I've found so far), I don't understand how to check for the presence of/retrieve for the amazon_metadata.asin field in the loop.

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?


More information about the colug-432 mailing list