Dynamoid / dynamoid

Ruby ORM for Amazon's DynamoDB.
MIT License
577 stars 197 forks source link

README.md: has_many pair with current implementation? #433

Open omarsotillo opened 4 years ago

omarsotillo commented 4 years ago

Hi! 👋

On the documentation it states the following:

Contrary to what you'd expect, association information is always contained on the object specifying the association, even if it seems like the association has a foreign key. This is a side effect of DynamoDB's structure: it's very difficult to find foreign keys without an index. Usually you won't find this to be a problem, but it does mean that association methods that build new models will not work correctly - for example, user.addresses.new returns an address that is not associated to the user. We'll be correcting this soon maybe someday, if we get a pull request.

👨‍💻 On the code, I decided to give it a try and it seems to work. E.g:

irb(main):006:0> LeadUrls.first.short_urls.create(slug: 'test', token: 'test', room: 'test', role: 'test')
=> #<ShortUrl:0x000055689bf02fe0 @new_record=false, @attributes={:slug=>"test", :token=>"test", :room=>"test", :role=>"test", :created_at=>Thu, 30 Apr 2020 07:26:43 +0000, :updated_at=>Thu, 30 Apr 2020 07:26:43 +0000}, @associations={}, @attributes_before_type_cast={:slug=>"test", :token=>"test", :room=>"test", :role=>"test", :created_at=>Thu, 30 Apr 2020 07:26:43 UTC +00:00, :updated_at=>Thu, 30 Apr 2020 07:26:43 UTC +00:00}, @changed_attributes={}, @previously_changed={"slug"=>[nil, "test"], "token"=>[nil, "test"], "room"=>[nil, "test"], "role"=>[nil, "test"], "created_at"=>[nil, Thu, 30 Apr 2020 07:26:43 +0000], "updated_at"=>[nil, Thu, 30 Apr 2020 07:26:43 +0000]}, @validation_context=nil, @errors=#<ActiveModel::Errors:0x000055689beeb548 @base=#<ShortUrl:0x000055689bf02fe0 ...>, @messages={}, @details={}>, @_touch_record=nil>

irb(main):007:0> LeadUrls.first.short_urls
=> #<Dynamoid::Associations::HasMany:0x000055689b7a9d20 @query={}, @name=:short_urls, @options={:class=>ShortUrl}, @source=#<LeadUrls:0x000055689b7f8ba0 @new_record=false, @attributes={:created_at=>Fri, 24 Apr 2020 06:59:51 +0000, :updated_at=>Thu, 30 Apr 2020 07:26:43 +0000, :lead_id=>1, :short_urls_ids=>#<Set: {"test"}>}, @associations={:short_urls_ids=>#<Dynamoid::Associations::HasMany:0x000055689b7a9d20 ...>}, @attributes_before_type_cast={:created_at=>Fri, 24 Apr 2020 06:59:51 +0000, :updated_at=>Thu, 30 Apr 2020 07:26:43 +0000, :lead_id=>1, :short_urls_ids=>#<Set: {"test"}>}, @changed_attributes={}, @previously_changed={}>, @loaded=false>

irb(main):008:0> LeadUrls.first.short_urls.count
=> 1

Model:


class LeadUrls
  include Dynamoid::Document

  table name: :lead_urls, key: :lead_id

  field :lead_id, :integer

  has_many :short_urls, class: ShortUrl
...
end
andrykonchin commented 4 years ago

Hmm. First of all thank you for the question.

I believe the issue still exists and it's related to association between persisted model and not persisted yet model. In your examples all the models are persisted that's why it works well.

Actually the documentation isn't accurate and there is no new method for has_many association so the example user.addresses.new doesn't work. There is a method << which adds a model to an association but it looks like it cannot add a not-persisted model.

Let's sum up. The mentioned issue exists and warning in documentation still actual but the documentation is not accurate.