我有一个 has_many 的问题:通过关联,我无法调用该u1.UsersProfileAttributes.find_by_ProfileAttribute_name("icq")
方法,rails 表示该方法不存在。该方法u1.UsersProfileAttributes.find_by_ProfileAttribute_id(3)
工作正常。u1 是一个用户对象。我不知道是什么问题,因为我的联想似乎没问题。看一看:
class ProfileAttribute < ActiveRecord::Base
has_many :UsersProfileAttributes
has_many :users, :through => :UsersProfileAttributes
end
class User < ActiveRecord::Base
has_many :UsersProfileAttributes
has_many :ProfileAttributes, :through => :UsersProfileAttributes
end
class UsersProfileAttribute < ActiveRecord::Base
belongs_to :user
belongs_to :ProfileAttribute
end
我的迁移文件:
class CreateProfileAttributes < ActiveRecord::Migration
def self.up
create_table :profile_attributes do |t|
t.string :name
t.integer :profile_group_id
t.timestamps
end
create_table :users_profile_attributes do |t|
t.integer :user_id
t.integer :ProfileAttribute_id
t.string :value
end
end
def self.down
drop_table :profile_attributes
drop_table :users_profile_attributes
end
end