Ruby on rails 问题有很多:通过RubyonRails中的关联
我对has\u many有一个问题:通过关联,我无法调用Ruby on rails 问题有很多:通过RubyonRails中的关联,ruby-on-rails,methods,associations,has-many,Ruby On Rails,Methods,Associations,Has Many,我对has\u many有一个问题:通过关联,我无法调用u1.UsersProfileAttributes.find\u by\u ProfileAttribute\u name(“icq”)方法,rails意味着该方法不存在。方法u1.UsersProfileAttributes.find\u by\u ProfileAttribute\u id(3)工作正常。u1是一个用户对象。我不知道是什么问题,因为我的联想似乎没问题。看看: class ProfileAttribute < Act
u1.UsersProfileAttributes.find\u by\u ProfileAttribute\u name(“icq”)
方法,rails意味着该方法不存在。方法u1.UsersProfileAttributes.find\u by\u ProfileAttribute\u 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 ProfileAttribute:usersProfIleaAttribute
结束
类用户:usersProfileAttribute
结束
类UsersProfileAttribute
我的迁移文件:
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
class CreateProfileAttributes
您需要查询ProfileAttribute,而不是UsersProfileAttribute。这应该适用于您:u1.ProfileAttributes.find\u by\u name('icq')
u1.UsersProfileAttributes.find\u by\u ProfileAttribute\u id(3)
起作用,因为ProfileAttribute\u id
是UsersProfileAttribute
的一个属性。。。所以ActiveRecord为您构建了一个动态查找器
u1.UsersProfileAttributes.find\u by\u ProfileAttribute\u name(“icq”)
不起作用,因为ProfileAttribute\u name
不是UsersProfileAttribute
的属性,谢谢,没错。另一个问题:有没有一个简单的方法来创造或改变这种关系。我现在就这样做:upa=UsersProfileAttribute?.new(:user=>u,:ProfileAttribute?=>pa,:value=>“profile-Wert”)u是一个用户对象,pa是一个ProfileAttribute对象。一行方法会很好,例如ProfileAttribute是自动生成的还是类似的?另一个问题:如何为特定用户更改已知ProfileAttribute的值?