Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 有很多:通过关联错误_Ruby On Rails_Associations - Fatal编程技术网

Ruby on rails 有很多:通过关联错误

Ruby on rails 有很多:通过关联错误,ruby-on-rails,associations,Ruby On Rails,Associations,我正在尝试设置一个简单的has\u many:through=>关联 我有三种型号。user.rb、icon.rb和user_icon.rb user.rb class User < ActiveRecord::Base has_many :user_icons has_many :icons, :through => :user_icons end 我得到这个错误 1.9.2-p290 :002 > User.first.icons ActiveRecord::Ha

我正在尝试设置一个简单的has\u many:through=>关联

我有三种型号。user.rb、icon.rb和user_icon.rb

user.rb

class User < ActiveRecord::Base
  has_many :user_icons
  has_many :icons, :through => :user_icons
end
我得到这个错误

1.9.2-p290 :002 > User.first.icons
ActiveRecord::HasManyThroughSourceAssociationNotFoundError: Could not find the source association(s) :icon or :icons in model UserIcon. Try 'has_many :icons, :through => :user_icons, :source => <name>'. Is it one of :User or :Icon?
    from /Users/jon/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.1/lib/active_record/reflection.rb:517:in `check_validity!'
    from /Users/jon/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.1/lib/active_record/associations/association.rb:27:in `initialize'
    from /Users/jon/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.1/lib/active_record/associations/collection_association.rb:24:in `initialize'
    from /Users/jon/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.1/lib/active_record/associations.rb:159:in `new'
    from /Users/jon/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.1/lib/active_record/associations.rb:159:in `association'
    from /Users/jon/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.1/lib/active_record/associations/builder/association.rb:41:in `block in define_readers'
    from (irb):2
    from /Users/jon/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.1/lib/rails/commands/console.rb:45:in `start'
    from /Users/jon/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.1/lib/rails/commands/console.rb:8:in `start'
    from /Users/jon/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.1/lib/rails/commands.rb:40:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
1.9.2-p290:002>User.first.icons
ActiveRecord::HasManyThroughSourceAssociationNotFoundError:在model UserIcon中找不到源关联:图标或:图标。试试“has_many:icons,:through=>:user_icons,:source=>”。它是:用户还是:图标之一?
from/Users/jon/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.1/lib/active\u record/reflection.rb:517:在“检查有效性”中
from/Users/jon/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.1/lib/active\u record/associations/association.rb:27:in“initialize”中
from/Users/jon/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.1/lib/active\u record/associations/collection\u association.rb:24:in'initialize'
from/Users/jon/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.1/lib/active_record/associations.rb:159:in“new”
from/Users/jon/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.1/lib/active_record/associations.rb:159:in“association”
from/Users/jon/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.1/lib/active\u record/associations/builder/association.rb:41:在“定义中的块”中
来自(irb):2
from/Users/jon/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.1/lib/rails/commands/console.rb:45:in'start'
from/Users/jon/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.1/lib/rails/commands/console.rb:8:in'start'
from/Users/jon/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.1/lib/rails/commands.rb:40:in`'
来自脚本/rails:6:in'require'
来自脚本/rails:6:in`'

知道我做错了什么吗

您的问题在于用户图标模型中的关联。案例在定义关联时很重要。如果您将模型更改为小写,它应该可以工作

class UserIcon < ActiveRecord::Base
  belongs_to :user
  belongs_to :icon
class用户图标
您的问题在于用户图标模型中的关联。案例在定义关联时很重要。如果您将模型更改为小写,它应该可以工作

class UserIcon < ActiveRecord::Base
  belongs_to :user
  belongs_to :icon
class用户图标
1.9.2-p290 :002 > User.first.icons
ActiveRecord::HasManyThroughSourceAssociationNotFoundError: Could not find the source association(s) :icon or :icons in model UserIcon. Try 'has_many :icons, :through => :user_icons, :source => <name>'. Is it one of :User or :Icon?
    from /Users/jon/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.1/lib/active_record/reflection.rb:517:in `check_validity!'
    from /Users/jon/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.1/lib/active_record/associations/association.rb:27:in `initialize'
    from /Users/jon/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.1/lib/active_record/associations/collection_association.rb:24:in `initialize'
    from /Users/jon/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.1/lib/active_record/associations.rb:159:in `new'
    from /Users/jon/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.1/lib/active_record/associations.rb:159:in `association'
    from /Users/jon/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.1/lib/active_record/associations/builder/association.rb:41:in `block in define_readers'
    from (irb):2
    from /Users/jon/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.1/lib/rails/commands/console.rb:45:in `start'
    from /Users/jon/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.1/lib/rails/commands/console.rb:8:in `start'
    from /Users/jon/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.1/lib/rails/commands.rb:40:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
class UserIcon < ActiveRecord::Base
  belongs_to :user
  belongs_to :icon