hyperactiveresource icon indicating copy to clipboard operation
hyperactiveresource copied to clipboard

active_record has_many, :through and active _resource

Open bitcoiners opened this issue 16 years ago • 4 comments

Hello, I have many to many relationship between Venue and Catalog

class Venue  :venue_catalogs
end

class VenueCatalog 

when I call url /venues.xml I get undefined method `quoted_table_name' for Catalog:Class

seems belongs_to HyperActiveResource is not recognizing catalog is not an ActiveRecord.

bitcoiners avatar Aug 25 '09 21:08 bitcoiners

Quick answer: HABTM relationships are not yet implemented. Same with the :through attribute.

Longer answer: Active Record makes the assumption that a HABTM object is another Active Record., and expects it to respond to stuff like "quoted table name" - it will, in fact, try to build a select statement using joins to get your catalogs... which is obviously not what you intend here.

I've not yet implemented HABTM or :through and haven't yet looked at how they are implemented as they're somewhat more complex than the simple relationships.

For now, all I can suggest is not trying to use :through on an Active Resource. Instead fetch the set of catalogs out of your venue_catalogs using a hand-coded method. Possibly something as simple as:

def catalogs
  venue_catalogs.map &:catalog
end

though I clearly haven't tested this code so it may not work - use with common sense ;)

Cheers, Taryn

taryneast avatar Aug 30 '09 22:08 taryneast

thanks for the tip. that worked. but i am puzzled by this line venue_catalogs.map &:catalog what does it do?

bitcoiners avatar Sep 17 '09 23:09 bitcoiners

is an equivalent of venue_catalog.map {|v| v.catalog }?

bitcoiners avatar Sep 17 '09 23:09 bitcoiners

Yep - it is just that. It's a bit of ruby magic that makes a Proc out of the given symbol. Really neat if all you're doing in the map is just a single, known, named method.

taryneast avatar Sep 18 '09 08:09 taryneast