简体   繁体   中英

Join two hash Tables (dynamoDb) with Ruby on Rails

I am new to Ruby for one project only - I need to join two tables with aws dynamodb. Basically the equivalent of sql left join. But since dynamodb apparently doesn't support I need to make it happen at the array level it seems.

Currently I am querying the one just fine, but I need to bring in this other table, but I'm having a heck of a time finding a simple example for ruby with rails without using ActiveRecord (to avoid causing an overhaul on pre-existing code).

client = Aws::DynamoDB::Client.new
response = client.scan(table_name: 'db_current')
@items = response.items

fake output to protect the innocent

db_current

{"machine_id"=>"pc-123435", "type_id"=>"t-56778"}

db_type

{"description"=>"Dell 5 Dev Computer", "Name"=>"Dell", "type_id"=>"t-56778"}

I thought I might have to make two:

 client = Aws::DynamoDB::Client.new
 db_c = client.scan(table_name: 'db_current')
 @c_items = db_c.items

 client = Aws::DynamoDB::Client.new
 db_t = client.scan(table_name: 'db_type')
 @t_items = db_c.joins(db_t['type_id'])  <=== then merge them

here.

 where I'll ultimately display description/name/machine_id

But sadly no luck.

I'm looking for suggestions. I'd prefer to keep it simple to really understand (It might sound unreasonable, I don't want to pull in ActiveRecord just yet unless I'll be owning this project going forward).

I ended up doing it this way. There is probably a more elegant solution for those that are familiar with Ruby... that I am not.

basically for each of the items in the first hash array (table), I use the ID from that one to filter on the item for the 2nd hash array. Merging them in the process. then appending to a final destination which I'll use for my UI.

    @c_by_id = Array.new

    @b_items.each do |item|
        pjoin = @c_items.first {|h| h['b_id'] == item['b_id']}
        newjoin = item.merge(pjoin)
        @c_by_id.append(newjoin)
    end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM