views:

25

answers:

1

I'm trying to do a many-to-many relationship in rails. It's my first try but I'm having a hard time to succeed. I want to be able to do @user.properties or @property.users.

#property.rb
has_many :ownages, :dependent => :destroy
has_many :users, :through => :ownages

#user.rb
has_many :ownages, :dependent => :destroy
has_many :properties, :through => :ownages

#ownages.rb
belongs_to :user
belongs_to :property

When I try this:

#SomeExampleController
p = Property.find_by_id(4)
p.users

I get:

NameError: uninitialized constant Property::Ownage

Same for this:

#SomeExampleController
u = User.find_by_id(1)
u.properties

This also gives me:

NameError: uninitialized constant User::Ownage

Anyone able to help me out? A big thank you in advance, I'm breaking my head over this. :-)

A: 

Hm, the ownages.rb is a typo? Your model name should be in singular. So:

class Ownage<Activerecord::Base
end

Rails automatically adds the plural ending to your model. Otherwise your relations look fine.

dombesz
I have no idea how this crept in, but this was indeed my error! Thank you very much, this fixed it for me.
Steven Fauconnier