views:

129

answers:

1

Hi, I've got a problem with a has_many :through association, i cant call the u1.UsersProfileAttributes.find_by_ProfileAttribute_name("icq") method, rails means that this method doesn't exist. The method u1.UsersProfileAttributes.find_by_ProfileAttribute_id(3) works correctly. u1 is a user object. I don't know whats the problem, because my associations seems to be okay. Have a look:

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

My Migration file:

    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
+1  A: 

You need to query ProfileAttributes, not UsersProfileAttributes. This should work for you: u1.ProfileAttributes.find_by_name('icq')

u1.UsersProfileAttributes.find_by_ProfileAttribute_id(3) works because ProfileAttribute_id is an attribute of UsersProfileAttribute ... so ActiveRecord builds you a dynamic finder.

u1.UsersProfileAttributes.find_by_ProfileAttribute_name("icq") does not work because ProfileAttribute_name is not an attribute of UsersProfileAttribute.

Dave Pirotte
Thanks, thats right. Another Questioen: Is there a simple methode to create or alter the releationship. Id do it like that now: upa = UsersProfileAttribute?.new(:user => u, :ProfileAttribute? => pa, :value => "Profil Wert") u is a user object and pa is a ProfileAttribute object. A 1 line methode would be nice, e.g. that the ProfileAttribute is generated automaticaly or something like that?
LeonS
Another Question: how can i alter the value of a known ProfileAttribute for a specific user?
LeonS