开发者

has_many :through association results in NameError

开发者 https://www.devze.com 2023-01-18 13:48 出处:网络
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.

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开发者_StackOverflow社区.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. :-)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消