Forum: Ruby on Rails has_many :through a belongs_to association

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Walther D. (Guest)
on 2008-10-14 18:27
I am on Rails 2.0.2 (not 2.1.x due to a couple of tickets) and if this
topic is invalid if only I upgraded, please excuse me for not being able
to have a perfect overview :(

My use case is: users are invited to events - and each event is
scheduled to 'perform' a number of times. This gives me the tables:
users, scheduled_events, event_users.

I have set up associations on the models like this:

- events has_many scheduled_events
- scheduled_events belongs_to event
- event_users belongs_to event and belongs_to user

Then I've tried to set up an association providing me with the scheduled
events to an invited user like this

- event_users has_many scheduled_events through event

But after googling about for the better part of a day and reading Josh
Susser extensively, I am lead to believe that has_many through only
works on has_many associations - ie. I'm not at liberty to go through a
belongs_to association.

Does anyone here have a take on how to solve this? Or am I perhaps just
messing this association thing up?

best regards,
Walt
Pratik N. (Guest)
on 2008-10-14 19:43
(Received via mailing list)
On Tue, Oct 14, 2008 at 4:27 PM, Walther D.
<removed_email_address@domain.invalid> wrote:
> But after googling about for the better part of a day and reading Josh
> Susser extensively, I am lead to believe that has_many through only
> works on has_many associations - ie. I'm not at liberty to go through a
> belongs_to association.
>
> Does anyone here have a take on how to solve this? Or am I perhaps just
> messing this association thing up?

This should work fine in Edge. Did you try to upgrade to edge ?

--
Cheers!
- Pratik
http://m.onkey.org
Walther D. (Guest)
on 2008-10-14 19:55
Pratik Naik wrote:
> On Tue, Oct 14, 2008 at 4:27 PM, Walther D.
> <removed_email_address@domain.invalid> wrote:
>> But after googling about for the better part of a day and reading Josh
>> Susser extensively, I am lead to believe that has_many through only
>> works on has_many associations - ie. I'm not at liberty to go through a
>> belongs_to association.
>>
>> Does anyone here have a take on how to solve this? Or am I perhaps just
>> messing this association thing up?
>
> This should work fine in Edge. Did you try to upgrade to edge ?
>
> --
> Cheers!
> - Pratik
> http://m.onkey.org

I'm overwhelmed - did not even dare to hope for any replies - and
certainly not this fast!

I am adding to a production site on 2.0.2 - and I have not been able to
find any good docs on how to upgrade to edge on my mac and still have my
2.0.2 development environment intact (it is _very_ important to me that
my existing 2.0.2 does work and will let me deploy to 2.0.2 production
sites)

Is that possible? Upgrading on my development mac - and still deploy to
2.0.2 production? And perhaps even - downgrading again - should the
upgrade turn out to hold to many TODOS?

thank you Pratik - I've come around your name on a great many posts,
tickets etc and I'm amased at the amount of work you put into this
community!

thank you again,
walt
Walther D. (Guest)
on 2008-10-15 00:35
Walther D. wrote:
> Pratik Naik wrote:
>> On Tue, Oct 14, 2008 at 4:27 PM, Walther D.
>> <removed_email_address@domain.invalid> wrote:
>>> But after googling about for the better part of a day and reading Josh
>>> Susser extensively, I am lead to believe that has_many through only
>>> works on has_many associations - ie. I'm not at liberty to go through a
>>> belongs_to association.
>>>
>>> Does anyone here have a take on how to solve this? Or am I perhaps just
>>> messing this association thing up?
>>
>> This should work fine in Edge. Did you try to upgrade to edge ?
>>
>> --
>> Cheers!
>> - Pratik
>> http://m.onkey.org
>
> I'm overwhelmed - did not even dare to hope for any replies - and
> certainly not this fast!
>
> I am adding to a production site on 2.0.2 - and I have not been able to
> find any good docs on how to upgrade to edge on my mac and still have my
> 2.0.2 development environment intact (it is _very_ important to me that
> my existing 2.0.2 does work and will let me deploy to 2.0.2 production
> sites)
>
> Is that possible? Upgrading on my development mac - and still deploy to
> 2.0.2 production? And perhaps even - downgrading again - should the
> upgrade turn out to hold to many TODOS?
>
> thank you Pratik - I've come around your name on a great many posts,
> tickets etc and I'm amased at the amount of work you put into this
> community!
>
> thank you again,
> walt

Pratik,

I'll try the "freeze rails"-thing and see if that will do the trick :)

thank you again,
walt
Walther D. (Guest)
on 2008-10-15 02:47
Walther D. wrote:
> Walther D. wrote:
>> Pratik Naik wrote:
>>> On Tue, Oct 14, 2008 at 4:27 PM, Walther D.
>>> <removed_email_address@domain.invalid> wrote:
>>>> But after googling about for the better part of a day and reading Josh
>>>> Susser extensively, I am lead to believe that has_many through only
>>>> works on has_many associations - ie. I'm not at liberty to go through a
>>>> belongs_to association.
>>>>
>>>> Does anyone here have a take on how to solve this? Or am I perhaps just
>>>> messing this association thing up?
>>>
>>> This should work fine in Edge. Did you try to upgrade to edge ?
>>>
>>> --
>>> Cheers!
>>> - Pratik
>>> http://m.onkey.org
>>
>> I'm overwhelmed - did not even dare to hope for any replies - and
>> certainly not this fast!
>>
>> I am adding to a production site on 2.0.2 - and I have not been able to
>> find any good docs on how to upgrade to edge on my mac and still have my
>> 2.0.2 development environment intact (it is _very_ important to me that
>> my existing 2.0.2 does work and will let me deploy to 2.0.2 production
>> sites)
>>
>> Is that possible? Upgrading on my development mac - and still deploy to
>> 2.0.2 production? And perhaps even - downgrading again - should the
>> upgrade turn out to hold to many TODOS?
>>
>> thank you Pratik - I've come around your name on a great many posts,
>> tickets etc and I'm amased at the amount of work you put into this
>> community!
>>
>> thank you again,
>> walt
>
> Pratik,
>
> I'll try the "freeze rails"-thing and see if that will do the trick :)
>
> thank you again,
> walt

'shoot - I'll better stay of the freezing path - what started out as a
small Just-Let-Me-Fix-That-Association-For-You turned into a
I-Cannot-Start-My-Project-Due-To-Missing-Methods-On-Core :(

So, I froze - and the project likewize <:)

First missing method is render_file - and I'm afraid that once I get
that 'son of gun nailed', edge has a queue of missing things built up
for me!

Too bad though - I really could have used that hmt belongs_to ...

I cloned the rails.git off of github into its own folder, then symlinked
it into vendor/rails on my project.

Then I did script/server --debugger as usual - and it chokes with:

../vendor/rails/activesupport/lib/active_support/core_ext/module/aliasing.rb:33:in
`alias_method': undefined method `render_file' for class
`ActionView::Base' (NameError)
Walther D. (Guest)
on 2008-10-15 05:05
>
> ../vendor/rails/activesupport/lib/active_support/core_ext/module/aliasing.rb:33:in
> `alias_method': undefined method `render_file' for class
> `ActionView::Base' (NameError)

Chalked this one up on me not keeping tabs on inherit_views plugin which
orignally manhandled the render_file method - upgrading that plugin had
me move on...

- to the next problem: mislav_will_paginate (which he will not in edge
due to:

/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems.rb:319:in
`activate':Gem::Exception: can't activate activesupport (>= 1.4.4),
already activated activesupport-2.2.0]

but that's not a problem - I commented these two dirty rotten
scoundrels:

gem 'mislav-will_paginate', '~> 2.2'
require 'will_paginate'

and off to the races, I was - in my script/console - eager to see the
promised land of has_many through a belongs_to.

Only to see this:

undefined method `quoted_table_name' for false:FalseClass
from
.../vendor/rails/activerecord/lib/active_record/associations/has_many_through_association.rb:123:in
`construct_conditions'

Before all this turns sour - I'm going to dump this branch - and hurry
back in the comforting shadows of 2.0.2 with my original plugins intact.

The has_many on a belongs_to?

I did the utmost unDRYness I could come u with:

on my user I did

-  has_many :event_users
-  has_many :invitations, :through => :event_users, :source => :event

and then on my events_controller - I stabbed this into the find_resource
enclosing_resource.invitations.collect { |i|
i.scheduled_events.month_year(fd,ld) }.flatten

case closed (for me - but I still shiver from the tour de force into the
freeze)!
Roman H. (Guest)
on 2008-10-16 14:42
(Received via mailing list)
Just had the same unpleasant experience with bleeding edge, looks like
too much trouble... wanted the upcoming internationalization backed
into Rails directly, though. Sigh...

On Oct 15, 3:05 am, Walther D. <rails-mailing-l...@andreas-
Roman H. (Guest)
on 2008-10-16 16:21
(Received via mailing list)
Just spent couple of hours debugging it.... Well, Rails-Edge alone
seems to work fine, so I figured out it should be some plugin,
deactivating them one at a time I found out, that it is

has_many_polymorphs,

that doesn't like Edge and has_many-through-Associations.

Well, maybe it's time to say goodbye? Guess, Evan from
http://blog.evanweaver.com/
doesn't have much time right now, working on Twitter.

Cheers,
Roman
Pratik N. (Guest)
on 2008-10-16 17:11
(Received via mailing list)
@Walt Hey, sorry I got busy and couldn't look at this thread.

@Roman : If you could mention the exact issues, I could fix
has_many_polymorphs plugin. Or if you can submit a patch, that works
the best!


On Thu, Oct 16, 2008 at 2:20 PM, Roman H.
<removed_email_address@domain.invalid> wrote:
> doesn't have much time right now, working on Twitter.
>>
>> > due to:
>>
>> > Before all this turns sour - I'm going to dump this branch - and hurry
>>
>> > and then on my events_controller - I stabbed this into the find_resource
>> > enclosing_resource.invitations.collect { |i|
>> > i.scheduled_events.month_year(fd,ld) }.flatten
>>
>> > case closed (for me - but I still shiver from the tour de force into the
>> > freeze)!
>> > --
>> > Posted viahttp://www.ruby-forum.com/.
> >
>



--
Cheers!
- Pratik
http://m.onkey.org
Roman H. (Guest)
on 2008-10-16 17:47
(Received via mailing list)
@Pratik: Thanks! I created a github project to showcase the issue:

http://github.com/mindreframer/hmp_and_edgerails_2...


Hope, that's OK.
Martin G. (Guest)
on 2008-10-17 19:20
(Received via mailing list)
Hi all,

I fixed this issue, it's available at
http://github.com/snusnu/has_many_polymorphs/tree/master

cheers
Martin
Roman H. (Guest)
on 2008-10-17 21:40
(Received via mailing list)
Thanks a lot!

Roman
Walther D. (Guest)
on 2008-10-18 04:59
Pratik Naik wrote:
> @Walt Hey, sorry I got busy and couldn't look at this thread.

@Pratik - no problem :)

I will have to feel my way into this edge business (and not bag a bundle
of plugins along on the ride) :D

But thank you even so for caring and your time!

@Martin G.

Thank you too! I have put your hmp top of my "clone-list" for when I
dare threading into deep freeze edge waters next time :D

best regards,
walt
edvin (Guest)
on 2008-10-28 10:12
(Received via mailing list)
Martin,

Your fix seems to address some of the issues, but calling the the
polymorphic association class still raises the same:

NoMethodError undefined method `macro' for false:FalseClass

For example

Site has Tags through Taggings

calling  Tag.taggables raises the above error

Any ideas?
ed (Guest)
on 2008-11-12 02:08
(Received via mailing list)
@edvin: Tag-related:
http://github.com/tejo/has_many_polymorphs/commit/...

I'm porting a 1.2.6 app to 2.2.0 and found that using Martin
Gamsjaeger's fork fixed most HMP issues though I still get the method
error for 'macro' as you do.  Can't quite figure it out.  Anyone else
get it?  I'm not seeing any forks on Github dealing with the error.

Ed
Nicholas H. (Guest)
on 2008-11-14 07:09
I've tried a couple of forks on Github as well with no luck. Would love
to see this work on Rails 2.2 as well.

Nicholas

ed wrote:
> @edvin: Tag-related:
> 
http://github.com/tejo/has_many_polymorphs/commit/...
>
> I'm porting a 1.2.6 app to 2.2.0 and found that using Martin
> Gamsjaeger's fork fixed most HMP issues though I still get the method
> error for 'macro' as you do.  Can't quite figure it out.  Anyone else
> get it?  I'm not seeing any forks on Github dealing with the error.
>
> Ed
jko170 (Guest)
on 2008-11-26 00:37
(Received via mailing list)
Anyone have a fix for this?

On Nov 13, 11:09 pm, Nicholas H. <removed_email_address@domain.invalid>
Martin G. (Guest)
on 2008-11-26 01:28
(Received via mailing list)
Hey,

Sorry I didn't get back to that thread for so long! I must have
overlooked it ... maybe because my fix is still working for me (using
rails-2.2.2, but using mostly my own additional methods). That said,
I'm not really sure how to reproduce the error you guys get. Maybe
it's a good idea to put a sample app that triggers the error up on
github? I could have a look tommorrow evening, although I must say
that I really don't know too much about the plugins code! I just
noticed a method in hmp that is an almost exact duplicate of a rails
method, and updated hmp's one to reflect the latest rails code (that
basically was the last fix).

Besides that, my commit got pulled back into the main repo at
http://github.com/fauna/has_many_polymorphs/tree/master recently.
Maybe this means that the original author might be available for
action :-)?

Anyways, a sample barebone rails-2.2.2 app on github that showcases
the error would help!

cheers
snusnu
Nick C. (Guest)
on 2008-11-27 12:28
I'm using the latest patched version of hmp, however I'm still receiving
this:

NoMethodError (undefined method `quoted_table_name' for
false:FalseClass):
    /vendor/rails/activerecord/lib/active_record/associations/has_many_through_association.rb:123:in
`construct_conditions'
    /vendor/rails/activerecord/lib/active_record/associations/has_many_through_association.rb:191:in
`construct_sql'
    /vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:21:in
`initialize'
    /vendor/rails/activerecord/lib/active_record/associations/has_many_through_association.rb:6:in
`initialize'
    /vendor/rails/activerecord/lib/active_record/associations.rb:1297:in
`new'
    /vendor/rails/activerecord/lib/active_record/associations.rb:1297:in
`groups'

Any ideas?

Thx
Nick


Martin G. wrote:
> Hey,
>
> Sorry I didn't get back to that thread for so long! I must have
> overlooked it ... maybe because my fix is still working for me (using
> rails-2.2.2, but using mostly my own additional methods). That said,
> I'm not really sure how to reproduce the error you guys get. Maybe
> it's a good idea to put a sample app that triggers the error up on
> github? I could have a look tommorrow evening, although I must say
> that I really don't know too much about the plugins code! I just
> noticed a method in hmp that is an almost exact duplicate of a rails
> method, and updated hmp's one to reflect the latest rails code (that
> basically was the last fix).
Niels (Guest)
on 2008-11-30 11:25
(Received via mailing list)
Nick C. (Guest)
on 2008-12-01 08:58
Thanks. All up and running now.

Niels wrote:
> This commit fixes the 2.2 compatibility issue:
> 
http://github.com/lennart/has_many_polymorphs/comm...
>
> Also see
> http://rubyforge.org/forum/forum.php?thread_id=295...
>
> - Niels.
This topic is locked and can not be replied to.