I am trying to call the joins method on my model, but whenever I do,
it always returns nil.
I have the following database schema:
========================
==== Host ====
id int
hostname nvarchar(50)
========================
==== Lsa ====
id int
host_id int
name nvachar(50)
email nvarchar(255)
I have the following model:
Host.rb
class Host < ActiveRecord::Base
set_table_name “systems.hosts”
has_one :lsainfo, :class_name => “LsaInfo”
end
LsaInfo.rb
class LsaInfo < ActiveRecord::Base
set_table_name “consolecheck.lsainfo”
belongs_to :host, :class_name => “Host”, :foreign_key =>
“host_id”, :primary_key => “id”
end
I am using rails 3.0.0 and executing commands from the rails
console. I am able to access the relations (I think this is the
correct term) with no problems, but as soon as I try to use joins, it
becomes a problem and I get a nil error.
Loading development environment (Rails 3.0.0)
Host.first.lsainfo
=> #<LsaInfo id: 28, host_id: 6, name:“Jeff Gonzalez”, email:
“[email protected]”>
LsaInfo.first.host
=> #<Host id: 51, hostname: “db”>
LsaInfo.joins(:host).to_sql
NoMethodError: undefined methodeq' for nil:NilClass from /Users/jeff/.gem/ruby/1.8/gems/activesupport-3.0.0/lib/ active_support/whiny_nil.rb:48:in
method_missing’
from /Users/jeff/.gem/ruby/1.8/gems/activerecord-3.0.0/lib/
active_record/associations.rb:2176:inassociation_join' from /Users/jeff/.gem/ruby/1.8/gems/activerecord-3.0.0/lib/ active_record/relation/query_methods.rb:209:in
build_joins’
from /Users/jeff/.gem/ruby/1.8/gems/activerecord-3.0.0/lib/
active_record/relation/query_methods.rb:204:ineach' from /Users/jeff/.gem/ruby/1.8/gems/activerecord-3.0.0/lib/ active_record/relation/query_methods.rb:204:in
build_joins’
from /Users/jeff/.gem/ruby/1.8/gems/activerecord-3.0.0/lib/
active_record/relation/query_methods.rb:138:inbuild_arel' from /Users/jeff/.gem/ruby/1.8/gems/activerecord-3.0.0/lib/ active_record/relation/query_methods.rb:110:in
arel’
from /Users/jeff/.gem/ruby/1.8/gems/activerecord-3.0.0/lib/
active_record/relation.rb:316:in `to_sql’
from (irb):6
Obviously I am not sure where my problem is occurring, or why things
are going wrong. I created a new rails project, and created the
category, post, comments, guest tables according to the Active Record
Query Interface documentation (found here:
Active Record Query Interface — Ruby on Rails Guides).
Once I added in some data, everything worked fine and I was able to
use the joins functionality without any issue in my new rails
project. However, I am still unable to get it working in my current
“real” project.
Any ideas on what I could be doing wrong?