Through relationship

I am working on streamlining one of my apps, and trying to wrap my
hands around through, polymorphic, and with_scope. All of these i
know i need for what i want to do, based on the little documentation
I have found. With this in mid I have tried to implement a simple
through in the app to try and get my head around it. This is what I
have:

class Product < ActiveRecord::Base
has_many :subscriptions
has_many :accounts, :through => :subscriptions
end

class Subscription < ActiveRecord::Base
belongs_to :product
belongs_to :account
end

class Account < ActiveRecord::Base
has_many :subscriptions
has_many :products, :through => :subscriptions
end

in my controller i have the following code:

@account = Account.find(@user.account_id)
@products = @account.products

but i recieve the following error:

You have a nil object when you didn’t expect it!
The error occured while evaluating nil.options
RAILS_ROOT: /Users/flip/Documents/workspace/SolidSuite/public/…/
config/…

Application Trace | Framework Trace | Full Trace
#{RAILS_ROOT}/vendor/rails/activerecord/lib/active_record/
associations/has_many_through_association.rb:66:in
construct_conditions' #{RAILS_ROOT}/vendor/rails/activerecord/lib/active_record/ associations/has_many_through_association.rb:7:ininitialize’
#{RAILS_ROOT}/vendor/rails/activerecord/lib/active_record/
associations.rb:695:in new' #{RAILS_ROOT}/vendor/rails/activerecord/lib/active_record/ associations.rb:695:inproducts’
#{RAILS_ROOT}/vendor/rails/activerecord/lib/active_record/
associations.rb:690:in products' #{RAILS_ROOT}/app/controllers/account_controller.rb:65:inproducts’

I updated my edge rails (doing it manual for many reasons). I am on
changeset: 3747 as far as I can tell however when i followed the
instructions on the wiki I cound not find a bin directory as the last
few steps mention in regards to replacing some files with newer
versions based on certain files on edge are not compatable with 1.0
files.

My db is mysql 5.0.18 and i have all the appropriate keys set. If i
remove the call to the through as well as the specific through call
and go back to the way I was doing it things work fine. Again I am
trying to do things very simple and than once I get the feel for it i
was going to optimize because I know what I am doing is not optimize.

Any help would be much appreciated.

As a side note another question is that I have tried this on a HABTM
relationship and get similar errors.

Thanks again

Andrew

Andrew,

I’m getting this same error and am wondering what I’m doing wrong
also. Not sure if it’s me or the code :slight_smile: If anyone else knows
what’s going on, please do pipe up.

Duane J.
(canadaduane)
http://blog.inquirylabs.com/

:through associations are working fine for me on edge. How are you
setting up your objects? Your model classes look right, but do you have
the objects hooked together properly? Like, did you create the
subscriptions?

I’ve been blogging a series on how to use :through associations in 1.1.
Check it out and see if it helps. If not let me know and maybe I can
help.

http://blog.hasmanythrough.com/articles/2006/02/28/association-goodness

–josh
blog.hasmanythrough.com

Andrew F. wrote:

I am working on streamlining one of my apps, and trying to wrap my
hands around through, polymorphic, and with_scope. All of these i
know i need for what i want to do, based on the little documentation
I have found. With this in mid I have tried to implement a simple
through in the app to try and get my head around it. This is what I
have:

class Product < ActiveRecord::Base
has_many :subscriptions
has_many :accounts, :through => :subscriptions
end

class Subscription < ActiveRecord::Base
belongs_to :product
belongs_to :account
end

class Account < ActiveRecord::Base
has_many :subscriptions
has_many :products, :through => :subscriptions
end

in my controller i have the following code:

@account = Account.find(@user.account_id)
@products = @account.products

but i recieve the following error:

You have a nil object when you didn’t expect it!
The error occured while evaluating nil.options
RAILS_ROOT: /Users/flip/Documents/workspace/SolidSuite/public/…/
config/…

Application Trace | Framework Trace | Full Trace
#{RAILS_ROOT}/vendor/rails/activerecord/lib/active_record/
associations/has_many_through_association.rb:66:in
construct_conditions' #{RAILS_ROOT}/vendor/rails/activerecord/lib/active_record/ associations/has_many_through_association.rb:7:in initialize’
#{RAILS_ROOT}/vendor/rails/activerecord/lib/active_record/
associations.rb:695:in new' #{RAILS_ROOT}/vendor/rails/activerecord/lib/active_record/ associations.rb:695:in products’
#{RAILS_ROOT}/vendor/rails/activerecord/lib/active_record/
associations.rb:690:in products' #{RAILS_ROOT}/app/controllers/account_controller.rb:65:in products’

I updated my edge rails (doing it manual for many reasons). I am on
changeset: 3747 as far as I can tell however when i followed the
instructions on the wiki I cound not find a bin directory as the last
few steps mention in regards to replacing some files with newer
versions based on certain files on edge are not compatable with 1.0
files.

My db is mysql 5.0.18 and i have all the appropriate keys set. If i
remove the call to the through as well as the specific through call
and go back to the way I was doing it things work fine. Again I am
trying to do things very simple and than once I get the feel for it i
was going to optimize because I know what I am doing is not optimize.

Any help would be much appreciated.

As a side note another question is that I have tried this on a HABTM
relationship and get similar errors.

Thanks again

Andrew