Forum: Ruby on Rails Through relationship

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
11bd67aedc131020a51440fad59116cc?d=identicon&s=25 Andrew Filipowski (Guest)
on 2006-03-03 02:39
(Received via mailing list)
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
D8cb8c8cd40ddf0cd05241443a591868?d=identicon&s=25 Duane Johnson (Guest)
on 2006-03-04 00:29
(Received via mailing list)
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 :)  If anyone else knows
what's going on, please do pipe up.

Duane Johnson
(canadaduane)
http://blog.inquirylabs.com/
9f0f89bbd9e1ecfbaab6584e429b7a2f?d=identicon&s=25 Josh Susser (jsusser)
on 2006-03-04 01:24
: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...

--josh
blog.hasmanythrough.com

Andrew Filipowski 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
This topic is locked and can not be replied to.