我的代码有3个模型,都有一个for belongs_to / has_many关联。当我尝试从我的对象中拉出表格时,它会抛出一个NoMethodError。与rails控制台和我的视图中的关联表。
有问题的模型:
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(&#34; btc&#34;)。networkhashrate或Network.find_by_coin_id(&#34; 83&#34;)。tag
我在我编写的另一个应用程序中几乎有相同的设置,并且它运行良好。我重新加载了rails console /重新启动了我的开发环境。为什么我没有看到我的关联?
感谢您抽出宝贵时间阅读本文。
答案 0 :(得分: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
的方法,硬币就是。