Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Row count sql fails when in a many-to-many relation, only when it's in a class method #225

Open
tominated opened this issue Sep 25, 2012 · 1 comment

Comments

@tominated
Copy link

Hey There,

I've got two tables (Posts and Tags), which are related many-to-many (using DMs auto-generated table). Not sure if I need to specify this, but the Tag table only has the name column (no id).

I wanted to make a class method to use in the json output that will return the amount of posts under a tag (so I can output the page count). I put the following method in the Post class:

def self.total_pages
  (self.count / 48).ceil
end

which works when calling Post.total_pages, but not when calling Tag.get('randomtagname'). The following error appears when trying the latter:

DataObjects::SyntaxError: ERROR:  column posts.tag_name does not exist
LINE 1: ...tags" ON "posts"."id" = "post_tags"."post_id" AND "posts"."t...
                                                             ^
 (code: 50360452, sql state: 42703, query: SELECT COUNT(*) FROM "posts" INNER JOIN "post_tags" ON "posts"."id" = "post_tags"."post_id" AND "posts"."tag_name" = 'purplegalaxies' INNER JOIN "tags" ON "post_tags"."tag_name" = "tags"."name" WHERE "post_tags"."tag_name" = 'purplegalaxies', uri: postgres:tom@localhost/hash_tagging?scheme=postgres&user=tom&password=&host=localhost&port=&path=/hash_tagging&query=&fragment=&adapter=postgres)
  from /Users/tom/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/dm-aggregates-1.2.0/lib/dm-aggregates/adapters/dm-do-adapter.rb:20:in `execute_reader'
  from /Users/tom/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/dm-aggregates-1.2.0/lib/dm-aggregates/adapters/dm-do-adapter.rb:20:in `block in aggregate'
  from /Users/tom/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/dm-do-adapter-1.2.0/lib/dm-do-adapter/adapter.rb:276:in `with_connection'
  from /Users/tom/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/dm-aggregates-1.2.0/lib/dm-aggregates/adapters/dm-do-adapter.rb:14:in `aggregate'
  from /Users/tom/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/dm-aggregates-1.2.0/lib/dm-aggregates/repository.rb:8:in `aggregate'
  from /Users/tom/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/dm-aggregates-1.2.0/lib/dm-aggregates/functions.rb:185:in `aggregate'
  from /Users/tom/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/dm-aggregates-1.2.0/lib/dm-aggregates/functions.rb:40:in `count'
  from /Users/tom/Dropbox/Web/blackmilk-repositories/hash-tagging/models.rb:34:in `total_pages'
  from /Users/tom/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/collection.rb:1466:in `block in delegate_to_model'
  from /Users/tom/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/model/scope.rb:53:in `block in with_scope'
  from /Users/tom/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/model/scope.rb:73:in `with_exclusive_scope'
  from /Users/tom/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/model/scope.rb:53:in `with_scope'
  from /Users/tom/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/collection.rb:1465:in `delegate_to_model'
  from /Users/tom/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/collection.rb:1444:in `method_missing'
  from (irb):4
  from /Users/tom/.rbenv/versions/1.9.3-p194/bin/irb:12:in `<main>'

I apologize in advance if this isn't a bug, and it's actually something I've done wrong.

@sheerun
Copy link

sheerun commented Oct 22, 2013

@tominated Could you post relevant parts of your models + relevant migrations?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants