[Feature] ActiveResource - Associations through reflections #230

wants to merge 4 commits into


None yet

SweeD commented Mar 17, 2011

Matthias Folz and I've added associations (has_many and has_one) via reflection classes to ActiveResources.

Here is the link to the googlegroups discussion:

And the lighthouse ticket:

I hope, it's not impolite to open a pull request without an answer on the lighthouse ticket form a core member.
If it's so: I'm sorry... ;)

Markus Schwed

SweeD commented Apr 26, 2011

This ticket would be the begin of extending active resource with more restful features.
(like association link resolving as seen in Atomic-feeds)


miloops commented Apr 27, 2011

What about belongs_to? If we add has_many and has_one is intuitive to have belongs_to associations, right?

SweeD commented Apr 27, 2011

Hi miloops,

in the first step, we've added just has_one and has_many associations to configurate the :class_name (more options will follow), which should be used for the initialization of associated objects.
(more detailed description in the linked Lighthouse ticket)

So, before it get merged, we should add a belongs_to association, that adds an accessor for the parent, which it belongs to?

Should we also add some autoresolving for a belongs_to association, if the request response (for i.e. Comment.find(2) ) only includes the 'post_id' instead of all attributes (nested) for the 'post' object. ?

@SweeD SweeD closed this Apr 27, 2011

@SweeD SweeD reopened this Apr 27, 2011

SweeD commented Apr 27, 2011

Sorry... i closed the pull request by mistake... :)


smartinez87 commented May 2, 2011

Please remember to include the guides updated with this change, to make it a complete request.

SweeD commented May 5, 2011

I just rebased to the latest rails master, so the least added commits aren't new commits.

+ # ====
+ #
+ # <tt>has_many :comments, :class_name => 'myblog/comment'</tt>
+ # Would resolve this comments into the <tt>Myblog::Comment</tt> class.

gogolok May 8, 2011

this -> those

+ #
+ # Associations in ActiveResource would be used to resolve nested attributes
+ # in a response with correct classes.
+ # Now they could be specify over Associations with the options :class_name

gogolok May 8, 2011

specify -> specified


guilleiguaran commented Jun 1, 2011

@SweeD: Do you finish belongs_to?

@core members: is there any chance you can review this for inclusion in Rails 3.2?

SweeD commented Jun 4, 2011

Sorry, I was on vacation.

We will finish it within the next two weeks.



sikachu commented Jun 15, 2011

You have two more days to complete your patch :P (haha, just j/k)

I'll come back and check again after you've finished it. If you can finish belongs_to, and squash some of the tiny commits that would be great. :)

@ghost ghost assigned sikachu Jun 15, 2011


guilleiguaran commented Jun 15, 2011

Just a friendly reminder, I keep an eye on this too, hahaha :D

SweeD commented Jun 22, 2011

@sikachu @guilleiguaran
Woohoo... haven't forgot.... We'll finish it.
Little bit stressful these days... :D

Please don't stone me... xD


guilleiguaran commented Jul 8, 2011

Looks like this is finished, I see belongs_to added on the code, I will start to test this now :)

@SweeD: great work!!!

SweeD commented Jul 9, 2011

Sorry, it's not finished, already.
But almost... ;)

Have to write the resolving of the parent for a belongs_to association and squash the tiny commits... :)
Will finish these days!

+module ActiveResource::Associations::Builder
+ class Association #:nodoc:
+ # providing a Class-Variable, which will have a differend store of subclasses

dasch Jul 9, 2011


"differend" -> "different"

+ autoload :HasMany, 'active_resource/associations/builder/has_many'
+ autoload :HasOne, 'active_resource/associations/builder/has_one'
+ autoload :BelongsTo, 'active_resource/associations/builder/belongs_to'
+ end

dasch Jul 9, 2011


Try to use autoload_at here, i.e.

module Builder
  autoload_at 'active_resource/associations/builder' do
    autoload :Association
    autoload :HasMany
    autoload :HasOne
    autoload :BelongsTo

jeremy commented Oct 9, 2011

Good idea and nice implementation. Targeting Rails 3.2.

+1 this is awesome.


sikachu commented Nov 29, 2011

@SweeD what's the progress of this patch? I want to make sure this can be merged into Rails 3.2 ;)


guilleiguaran commented Nov 30, 2011

+1, this should be in master soon :)

SweeD commented Dec 2, 2011

Oha, haven't got time for a while.

Will finish it this month, sorry about that. ;)

SweeD commented Dec 2, 2011

@sikachu we "just" have to finish the belongs_to association and then, we're done.


jeremy commented Dec 14, 2011

Have to write the resolving of the parent for a belongs_to association and squash the tiny commits...

Rails 3.2 release candidate is coming within days, so this is going to miss the boat without a final push!


sikachu commented Dec 14, 2011


SweeD commented Dec 15, 2011

We will work on it today and hopefully finish it tomorrow.

Noting that the last comment is from Dec 15th with a hopeful finish by Dec 16th. Just wanted to bump this to see if the status has changed.

SweeD commented Feb 8, 2012

..but sadly, the status haven't changed since 15.12.

But we will finish it 100% this weekend.

SweeD commented Feb 12, 2012

@sikachu ....and finally: Done! :D

Really, really sorry for the delay.
Looking forward to feedback... :)


jeremy commented Mar 3, 2012

@SweeD great!

SweeD commented Mar 5, 2012

@jeremy Thanks!

Something to change/cleanup or so?

SweeD commented Mar 14, 2012

Closed for new pull request in new rails/activeresource repo.


Oh... it's the first one... xD

@SweeD SweeD closed this Mar 14, 2012

@SweeD SweeD referenced this pull request in rails/activeresource Oct 18, 2013


[Feature] ActiveResource - Associations through reflections #1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment