Find by include nil object error in rails

前端 未结 3 1659
猫巷女王i
猫巷女王i 2021-01-24 03:22

I\'ve been trying hard to solve this problem but i really don\'t know what is happening. I have this small piece of code :

DiscoveredLocation.find_by_user_id(use         


        
相关标签:
3条回答
  • 2021-01-24 03:54

    The DiscoveredLocation model is attempting to define a nested has-many through association:

    DiscoveredLocation  has_many :monsters, :through => :boss_location
    DiscoveredLocation  has_many :boss_kills, :through => :monsters
    

    I don't believe ActiveRecord can do this out of the box. There is a nested_has_many_through plugin which claims to support this -- I have no idea if it would work in this case.

    More discussion can be found here and here

    0 讨论(0)
  • 2021-01-24 03:55

    I could be wrong, however to help you out, try DiscoveredLocation.find_by_user_id(user.id, :include => [:boss_kill])

    Lastly, are you sure you are passing a variable called user into the controller? maybe current_user, if you are using a popular auth gem?

    0 讨论(0)
  • 2021-01-24 04:03

    You need to include the intermediate tables as well, otherwise there's no way to map boss_kills to DiscoveredLocations:

    DiscoveredLocation.find_by_user_id(user.id, :include => [:monsters, :boss_locations,:boss_kills])
    

    Also, check your development log to see the query that gets generated. If possible, post it here so we can determine whether it's missing one of the intermediate tables.

    0 讨论(0)
提交回复
热议问题