0

has_many:throughアソシエーションに問題があり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
4

1 に答える 1

2

UsersProfileAttributesではなく、ProfileAttributesを照会する必要があります。これはあなたのために働くはずです:u1.ProfileAttributes.find_by_name('icq')

u1.UsersProfileAttributes.find_by_ProfileAttribute_id(3)ProfileAttribute_idは...の属性であるため、UsersProfileAttribute機能します。したがって、ActiveRecordは動的ファインダーを構築します。

u1.UsersProfileAttributes.find_by_ProfileAttribute_name("icq")ProfileAttribute_nameはの属性ではないため、機能しませんUsersProfileAttribute

于 2009-12-20T19:29:04.783 に答える