dynamoid icon indicating copy to clipboard operation
dynamoid copied to clipboard

Resolve associations?

Open gaiottino opened this issue 8 years ago • 6 comments

Hi,

It's not clear from the documentation, but using the example from another question

class User
  include Dynamoid::Document

  field :name

  has_many :comments
end

class Comment
  include Dynamoid::Document

  field :body

  belongs_to :user
end

calling user.comments returns an association on which you can call to_a or user.comments.map(&:body) which feels reasonable.

But when calling comment.user.name you get the name of the association and not the user.

comment.user
 => #<Dynamoid::Associations::BelongsTo:0x007fd32faa98d0 @name=:user, @options={}, @source=#<Comment:0x007fd33011a228 @new_record=false, @attributes={:created_at=>Wed, 30 Aug 2017 10:39:09 +0200, :updated_at=>Wed, 30 Aug 2017 10:39:09 +0200, :id=>"980b9074-76ce-4a00-9822-18168e20cd75", :body=>"TEST", :user_ids=>#<Set: {"67cb9338-a64e-479c-9f46-438f653e5a4a"}>}, @associations={:user_ids=>#<Dynamoid::Associations::BelongsTo:0x007fd32faa98d0 ...>}, @changed_attributes={}>, @loaded=false>
comment.user.name
 => :user
comment.user.target.name
 => "TEST"

You seem to have to call #target before being able to call #name which is not what I expected from having worked with other ORMs compatible with ActiveRecord.

gaiottino avatar Aug 30 '17 08:08 gaiottino

Yeah, all model methods are available on proxy association object except several: name, options, source, loaded etc.

Looks like it's a specific of belongs_to and has_one associations only.

I may assume the expected behaviour (for single association) is to return model immediately, not proxy object.

@pboling @richardhsu what do you think?

andrykonchin avatar Sep 04 '17 09:09 andrykonchin

@richardhsu @andrykonchin @gaiottino The described, current, behavior sounds less than ideal to me. I believe rails hides the association proxy object, inside the target somewhere, so the target can be the one directly exposed. I would prefer that we keep it more like rails.

Also think we should get master released before a change like that though, as that would be a major version bump.

In order to release master though we need to update the CHANGELOG, and I haven't had time to review the commits that have gone in since last release.

pboling avatar Sep 04 '17 19:09 pboling

Hi, can somebody give an advice about belongs_to

I have a model with

  class Event
    belongs_to :event_profile, class_name: 'EventProfile',
                               primary_key: :external_user_id, foreign_key: :external_id
    field :external_user_id
  end

Event.first.event_profile.target is always nil, but I do EventProfile.where(external_id: Event.first.external_user_id).first all good any thoughts about? Thanks

vitalinfo avatar Jun 06 '18 16:06 vitalinfo

Could you please provide minimal code snippet to reproduce the issue?

Some notes:

  • primary_key option isn't supported
  • looks like Event#external_user_id is a foreign key but you specified another field external_id

andrykonchin avatar Jun 08 '18 08:06 andrykonchin

@andrykonchin I see, primary_key isn't supported it shouldn't work in my case

vitalinfo avatar Jun 11 '18 09:06 vitalinfo

@vitalinfo Model takes primary id of associated model from the metadata. If you use not standard name id, you have to specify it in the EventProfile model with

  table :key => :external_id

andrykonchin avatar Jun 11 '18 10:06 andrykonchin