【问题标题】:belongs_to / has_many association throwing NoMethodError: undefined methodbelongs_to / has_many 关联抛出 NoMethodError: undefined method
【发布时间】:2014-04-03 10:16:15
【问题描述】:

我的代码有 3 个模型,所有模型都有一个 for belongs_to / has_many 关联。当我尝试使用 rails 控制台和视图从对象的关联表中提取表时,它会抛出 NoMethodError。

有问题的型号:

class Coin < ActiveRecord::Base
  # If coin is destroyed, so is their market prices
  has_many :market_prices, dependent: :destroy
  has_many :networks, dependent: :destroy
end

class MarketPrice < ActiveRecord::Base
  belongs_to :coin
end

class Network < ActiveRecord::Base
  belongs_to :coin
end

Rails 控制台:

2.0.0-p247 :004 > Coin.find_by_tag("btc").networkhashrate                                                          Coin Load (0.3ms)  SELECT "coins".* FROM "coins" WHERE "coins"."tag" = 'btc' LIMIT 1                                                                                                                     
NoMethodError: undefined method `networkhashrate' for #<Coin:0x00000005b15030>                                                                                                                             
        from /home/action/.rvm/gems/ruby-2.0.0-p247/gems/activemodel-4.0.0/lib/active_model/attribute_methods.rb:436:in `method_missing'                                                                   
        from /home/action/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/attribute_methods.rb:131:in `method_missing'                                                                 
        from (irb):4                                                                                                                                                                                       
        from /home/action/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'                                                                                        
        from /home/action/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'                                                                                         
        from /home/action/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'                                                                                     
        from bin/rails:4:in `require'                                                                                                                                                                      
        from bin/rails:4:in `<main>'   

另一个例子:

2.0.0-p247 :005 > coin = Coin.first                                                                                                                                                                        
  Coin Load (0.3ms)  SELECT "coins".* FROM "coins" ORDER BY "coins"."id" ASC LIMIT 1                                                                                                                       
 => #<Coin id: 1, created_at: "2014-04-02 02:44:01", updated_at: "2014-04-02 02:44:01", tag: "10-5", name: "10-5", website: "">                                                                            
2.0.0-p247 :006 > coin.coin_id                                                                                                                                                                             
NoMethodError: undefined method `coin_id' for #<Coin:0x00000005b1c920>                                                                                                                                     
        from /home/action/.rvm/gems/ruby-2.0.0-p247/gems/activemodel-4.0.0/lib/active_model/attribute_methods.rb:436:in `method_missing'                                                                   
        from /home/action/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/attribute_methods.rb:131:in `method_missing'                                                                 
        from (irb):6                                                                                                                                                                                       
        from /home/action/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'                                                                                        
        from /home/action/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'                                                                                         
        from /home/action/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'                                                                                     
        from bin/rails:4:in `require'                                                                                                                                                                      
        from bin/rails:4:in `<main>' 

Schema.rb:

ActiveRecord::Schema.define(version: 20140402005453) do

  create_table "coins", force: true do |t|
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "tag"
  end

  create_table "market_prices", force: true do |t|
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "coin_id"        
  end

  create_table "networks", force: true do |t|
    t.integer  "networkhashrate"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "coin_id"
  end

end

我无法朝任何一个方向前进。示例:Coin.find_by_tag("btc").networkhashrate 或 Network.find_by_coin_id("83").tag

我在另一个我编写的应用程序中有几乎相同的示例设置,它运行良好。我已经重新加载了 rails 控制台/重新启动了我的开发环境。为什么 rails 看不到我的关联?

感谢您抽出宝贵时间阅读本文。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 model-associations


    【解决方案1】:

    Coin.find_by_tag("btc").networkhashrate

    Coin.find_by_tag 返回一个硬币。硬币没有称为 networkhashrate 的方法,网络有。


    Network.find_by_coin_id("83").tag

    Network.find_by_coin_id 返回一个网络。网络没有称为tag 的方法,硬币有。

    【讨论】:

    • 添加:Coin.find_by_tag("btc").networks.map(&amp;:networkhashrate) 应该有效,Network.find_by_coin_id("83").coin.tag 也应该有效; coin.coin_id 应该是 coin.id
    • 感谢您的回复。你能解释一下为什么我不能做 Coin.find_by_tag("btc").networks.networkhashrate 但类似的语法适用于 Network.find_by_coin_id("83").coin.tag?
    • 第一个例子使用networks,它返回一个网络集合,第二个例子使用coin,它返回一个Coin实例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-09
    相关资源
    最近更新 更多