2016-12-22 1 views
0

Ich habe zwei Modelle (eins zu viele Beziehung zwischen ihnen) in Schienen 5, Man ist ScholarshipGroup und Stipendienmodell mit Admin-Namespace.Rails konnte die umgekehrte Zuordnung nicht finden in hat viele Relation

ScholarshipGroup Modell:

class Admin::ScholarshipGroup < ApplicationRecord  
    has_many :admin_scholarships, :class_name => 'Admin::Scholarship',inverse_of: :admin_scholarship_group 
end 

und Wissenschaft Modell:

class Admin::Scholarship < ApplicationRecord 
    belongs_to :group, :class_name=> 'Admin::ScholarshipGroup', inverse_of: 'admin_scholarships'  
end 

Ich möchte Daten für alle Stipendien im Rahmen eines ScholarshipGroup benötigen. Aber wenn ich diese Abfrage ausführen von Schienen Konsole:

$ Admin::ScholarshipGroup.first.admin_scholarships.first 

Aber es gibt mir diese Fehlermeldung:

Admin::ScholarshipGroup Load (0.3ms) SELECT "admin_scholarship_groups".* FROM "admin_scholarship_groups" ORDER BY "admin_scholarship_groups"."id" ASC LIMIT ? [["LIMIT", 1]] 
ActiveRecord::InverseOfAssociationNotFoundError: Could not find the inverse association for admin_scholarships (:admin_scholarship_group in Admin::Scholarship) 
     from /usr/local/rvm/gems/[email protected]/gems/activerecord-5.0.0.1/lib/active_record/reflection.rb:202:in `check_validity_of_inverse!' 
     from /usr/local/rvm/gems/[email protected]/gems/activerecord-5.0.0.1/lib/active_record/reflection.rb:402:in `check_validity!' 
     from /usr/local/rvm/gems/[email protected]/gems/activerecord-5.0.0.1/lib/active_record/associations/association.rb:25:in `initialize' 
     from /usr/local/rvm/gems/[email protected]/gems/activerecord-5.0.0.1/lib/active_record/associations.rb:235:in `new' 
     from /usr/local/rvm/gems/[email protected]/gems/activerecord-5.0.0.1/lib/active_record/associations.rb:235:in `association' 
     from /usr/local/rvm/gems/[email protected]/gems/activerecord-5.0.0.1/lib/active_record/associations/builder/association.rb:111:in `admin_scholarships' 
     from (irb):34 
     from /usr/local/rvm/gems/[email protected]/gems/railties-5.0.0.1/lib/rails/commands/console.rb:65:in `start' 
     from /usr/local/rvm/gems/[email protected]/gems/railties-5.0.0.1/lib/rails/commands/console_helper.rb:9:in `start' 
     from /usr/local/rvm/gems/[email protected]/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:78:in `console' 
     from /usr/local/rvm/gems/[email protected]/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:49:in `run_command!' 
     from /usr/local/rvm/gems/[email protected]/gems/railties-5.0.0.1/lib/rails/commands.rb:18:in `<top (required)>' 
     from /usr/local/rvm/gems/ruby-2.3.[email protected]/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `require' 
     from /usr/local/rvm/gems/[email protected]/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `block in require' 
     from /usr/local/rvm/gems/[email protected]/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:259:in `load_dependency' 
     from /usr/local/rvm/gems/[email protected]/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `require' 
     from /home/ubuntu/workspace/bin/rails:9:in `<top (required)>' 
     from /usr/local/rvm/gems/[email protected]/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:287:in `load' 
     from /usr/local/rvm/gems/[email protected]/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:287:in `block in load' 
     from /usr/local/rvm/gems/[email protected]/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:259:in `load_dependency' 
     from /usr/local/rvm/gems/[email protected]/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:287:in `load' 
     from /usr/local/rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require' 
     from /usr/local/rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require' 
     from -e:1:in `<main>' 

Warum diese Art Fehler kehren? Kann mir jemand die Lösung dieses Fehlers vorschlagen?

Antwort

4

Sie definierten belongs_to :group in Admin::Scholarship Modell.

können Sie versuchen:

has_many :admin_scholarships, :class_name => 'Admin::Scholarship',inverse_of: :group, foreign_key: 'group_id' 
+0

Ich habe Änderung haben, aber jetzt zeigt es mir diese Fehlermeldung:. ' Admin :: ScholarshipGroup Load (0,5 ms) SELECT "admin_scholarship_groups" * FROM "admin_scholarship_groups" ORDER Von "admin_scholarship_groups". "Id" ASC LIMIT? [["LIMIT", 1]] Admin :: Scholarship Load (0.7ms) WÄHLEN Sie "admin_scholarships". * FROM "admin_scholarships" WHERE "admin_scholarships". "Stipendial_group_id" =? ORDER BY "admin_scholarships". "Id" ASC LIMIT? [["stipendium_group_id", 1], ["LIMIT", 1]] ' –

+0

Was ist der Fehler @ Engr.TanbirHasan? – Thanh

+0

Dieser Fehler: ActiveRecord :: StatementInvalid: SQLite3 :: SQLException: keine solche Spalte: admin_scholarships.scholarship_group_id –

Verwandte Themen