Skip to content

Instantly share code, notes, and snippets.

@ybart
Created June 5, 2011 01:43
Show Gist options
  • Save ybart/1008566 to your computer and use it in GitHub Desktop.
Save ybart/1008566 to your computer and use it in GitHub Desktop.
>> Ranking.all(:order => Ranking.clip.name.asc, :links => [:clip])
DataObjects::SyntaxError: no such column: clips.id (code: 1, sql state: , query: SELECT "rankings"."id", "rankings"."rank", "rankings"."progress", "rankings"."vote_count", "rankings"."votes", "rankings"."score", "rankings"."total", "rankings"."clip_id", "rankings"."jtop_id" FROM "rankings" INNER JOIN "rankings" "rankings_1" ON "clips"."id" = "rankings_1"."clip_id" ORDER BY "clips"."name", uri: sqlite3:/Users/ybart/Documents/Development/Rails/JTopStats/db/JTopStats_development.db?adapter=sqlite3&path=/Users/ybart/Documents/Development/Rails/JTopStats/db/JTopStats_development.db)
from /Library/Ruby/Gems/1.8/gems/dm-do-adapter-1.1.0/lib/dm-do-adapter/adapter.rb:147:in `execute_reader'
from /Library/Ruby/Gems/1.8/gems/dm-do-adapter-1.1.0/lib/dm-do-adapter/adapter.rb:147:in `read'
from /Library/Ruby/Gems/1.8/gems/dm-do-adapter-1.1.0/lib/dm-do-adapter/adapter.rb:276:in `with_connection'
from /Library/Ruby/Gems/1.8/gems/dm-do-adapter-1.1.0/lib/dm-do-adapter/adapter.rb:141:in `read'
from /Library/Ruby/Gems/1.8/gems/dm-core-1.1.0/lib/dm-core/repository.rb:162:in `read'
from /Library/Ruby/Gems/1.8/gems/dm-core-1.1.0/lib/dm-core/collection.rb:1117:in `lazy_load'
from /Library/Ruby/Gems/1.8/gems/dm-core-1.1.0/lib/dm-core/support/lazy_array.rb:409:in `each'
from /Library/Ruby/Gems/1.8/gems/dm-core-1.1.0/lib/dm-core/collection.rb:504:in `each'
from /Library/Ruby/Gems/1.8/gems/dm-core-1.1.0/lib/dm-core/collection.rb:976:in `map'
from /Library/Ruby/Gems/1.8/gems/dm-core-1.1.0/lib/dm-core/collection.rb:976:in `inspect'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/irb.rb:302:in `output_value'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/irb.rb:151:in `eval_input'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/irb.rb:263:in `signal_status'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/irb.rb:147:in `eval_input'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/irb.rb:146:in `eval_input'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/irb.rb:70:in `start'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/irb.rb:69:in `catch'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/irb.rb:69:in `start'
from /Library/Ruby/Gems/1.8/gems/railties-3.0.7/lib/rails/commands/console.rb:44:in `start'
from /Library/Ruby/Gems/1.8/gems/railties-3.0.7/lib/rails/commands/console.rb:8:in `start'
from /Library/Ruby/Gems/1.8/gems/railties-3.0.7/lib/rails/commands.rb:23
from script/rails:6:in `require'
from script/rails:6>>
@ybart
Copy link
Author

ybart commented Jun 5, 2011

class Ranking

  include DataMapper::Resource

  property :id, Serial

  property :rank, Integer
  property :progress, Integer
  property :vote_count, Integer
  property :votes, Integer
  property :score, Integer
  property :total, Integer

  belongs_to :clip
  belongs_to :jtop

  has 1, :artist, :through => :clip  
end

@ybart
Copy link
Author

ybart commented Jun 5, 2011

class Clip

  include DataMapper::Resource

  property :id, Serial

  property :name, String
  property :featuring, String

  belongs_to :artist
end

@d11wtq
Copy link

d11wtq commented Jun 5, 2011

Try defining has n, :rankings in Clip. Although I'm not clear if this is supposed to be one-to-one or one-to-many. Does a clip only ever have one ranking?

@ybart
Copy link
Author

ybart commented Jun 5, 2011

I've found a blog post that helped me to solve my problem here. http://rhnh.net/2010/12/01/ordering-by-a-field-in-a-join-model-with-datamapper.

  def self.sorted_by_clips direction=:asc
    order = DataMapper::Query::Direction.new(clip.artist.name, direction)
    order2 = DataMapper::Query::Direction.new(clip.name, direction)
    query = all.query
    query.instance_variable_set("@order", [order, order2])
    query.instance_variable_set("@links", [relationships['clip'].inverse, Clip.relationships['artist'].inverse])
    all(query)
  end

I query it this way :

  def rankings
    if params[:c] == 'clip'
      if params[:d] == 'down'
        Ranking.sorted_by_clips(:desc).all(:jtop_id => @jtop.id)
      else
        Ranking.sorted_by_clips.all(:jtop_id => @jtop.id)
      end
    else
      @jtop.rankings(:order => sort_order)
    end
  end

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