My code has 3 models, all with a for belongs_to / has_many association. It is throwing a NoMethodError when I try to pull tables from my objects' association table with rails console and in my views.
Models in question:
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 console:
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>'
Another example:
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
I'm not able to go in either direction. Example: Coin.find_by_tag("btc").networkhashrate or Network.find_by_coin_id("83").tag
I have almost the example same setup in another app I have written and it works perfectly. I've reloaded rails console / restarted my dev environment. Why isn't rails seeing my association I have?
Thank you for taking the time to read this.
Coin.find_by_tag("btc").networkhashrate
Coin.find_by_tag
returns a Coin. Coins don't have a method called networkhashrate, networks do.
Network.find_by_coin_id("83").tag
Network.find_by_coin_id
returns a Network. Networks don't have a method called tag
, coins do.