开发者

:order does not work on :include

开发者 https://www.devze.com 2023-02-01 22:49 出处:网络
i\'m wondering why this gives me an error : DiscoveredLocation.find_all_by_user_id(user.id, :include => [:boss_location, :monsters], :order => \'boss_location.location_index ASC\')

i'm wondering why this gives me an error :

DiscoveredLocation.find_all_by_user_id(user.id, :include => [:boss_location, :monsters], :order => 'boss_location.location_index ASC')

It seems as if it's trying to execute a really long query and i get an error like :

Mysql::Error: Unknown column 'monsters_discovered_locations_join.boss_location_id' in 'on clause': SELECT `discovered_locations`.`id` AS t0_r0, `discovered_locations`.`user_id` AS t0_r1, `discovered_locations`.`boss_location_id` AS t0_r2, `discovered_locations`.`created_at` AS t0_r3, `discovered_locations`.`updated_at` AS t0_r4, `boss_locations`.`id` AS t1_r0, `boss_locations`.`name` AS t1_r1, `boss_locations`.`location_index` AS t1_r2, `boss_locations`.`min_level` AS t1_r3, `boss_locations`.`needed_gold_to_open` AS t1_r4, `boss_locations`.`created_at` AS t1_r5, `boss_locations`.`updated_at` AS t1_r6, `monsters`.`id` AS t2_r0, `monsters`.`name` AS t2_r1, `monsters`.`strength` AS t2_r2, `monsters`.`dexterity` AS t2_r3, `monsters`.`magic` AS t2_r4, `monsters`.`accuracy` AS t2_r5, `monsters`.`minGold` AS t2_r6, `monsters`.`maxGold` AS t2_r7, `monsters`.`hp` AS t2_r8, `monsters`.`level` AS t2_r9, `monsters`.`armor` AS t2_r10, `monsters`.`first_class` AS t2_r11, `monsters`.`weapon_id` AS t2_r12, `monsters`.`imageName` AS t2_r13, `monsters`.`monster_type` AS t2_r14, `monsters`.`boss_location_index` AS t2_r15, `monsters`.`boss_location_id` AS t2_r16, `monsters`.`created_at` AS t2_r17, `monsters`.`updated_at` AS t2_r18 FROM `discovered_locations`  LEFT OUTER JOIN `boss_locations` ON `boss_locations`.id = `discovered_locations`.boss_location_id  LEFT OUTER JOIN `boss_locations` monsters_discovered_locations_join ON (`discovered_locations`.`id` = `monsters_discovered_locations_join`.`boss_location_id`)  LEFT OUTER JOIN `monsters` ON (`monsters`.`boss_location_id` = `monsters_discovered_locations_join`.`id`) WHERE (`discovered_locations`.`user_id` = 986759322)  ORDER BY boss_location.location_index ASC

The models associations are :

class BossKill < ActiveRecord::Base

    belongs_to :user
    belongs_to :monster

class DiscoveredLocation < ActiveRecord::Base
  belongs_to :user
  belongs_to :boss_location

  has_many :monsters, :through => :boss_location

  has_many :boss_kills, :through => :monsters

class BossLocation < ActiveRecord::Base

  has_many :discovered_locations
  has_many :users, :through => :discovered_locations

  has_many :monsters

class Monster < ActiveRecord::Base
  belongs_to :boss_loc开发者_开发知识库ation

  has_many :boss_kills

Any ideas ?


Right... Because include INCLUDES - It uses agressive loading, unlike joins. There is nothing wrong with the behavior here. Change :include to :joins and you'll get your desired result, but less efficiently

DiscoveredLocation.find(
    :all, 
    :conditions => {
        :user_id => user.id
    }, 
    :joins => :boss_locations, 
    :include => :monsters, 
    :order => 'boss_locations.location.index'
)

Try that - REMEMBER - MODEL names are singluar, but TABLE names are plural (boss_location vs boss_locations). If you read your logs/errors, you'd see this


This Trick May be helpful: DiscoveredLocation.find(:all,:conditions => {:user_id => user.id},:joins => :boss_locations,:include => :monsters).order((boss_locations.location.index): :desc)

0

精彩评论

暂无评论...
验证码 换一张
取 消