How to relate model with self value?

Hi,
I'm developing with ruby-1.9. 1 rails 3.0.0.
I want to relate 'User' model and 'Payment' model with condition which
has the same value as 'user.testing' in 'payment.testing'

I did

Class User < ActiveRecord::Base
    has_many payments, :conditions => ['testing = ?', self.testing]
end

This doesn't work.
I tried also below, but doesn't work too.
has_many payments, :conditions => ['testing = ?',
#{self.send(:testing)}]

Any ideas?

Regards.

You could just use a method instead…

class User < ActiveRecord::Base

has_many :payments

def testing_payments
payments.where(:testing => testing)
end

end

That would be even better as a named scope.

Colin

Actually, it would be better if it worked:

class User < ActiveRecord::Base
   has_many :payments

   def testing_payments
     payments.where(:testing => true) # testing is a local?
   end
end

somebody = User.find(params[:id])

Then, you could say:
  somebody.testing_payments

Or add a scope to Payment

class Payment < ActiveRecord::Base
   belongs_to :user

   scope :testing, where(:testing => true)
end

And you'd say:
  somebody.payments.testing

Either would work. If testing is a instance method of User, then the original suggestion might actually be "better."

-Rob

Rob Biedenharn
Rob@AgileConsultingLLC.com http://AgileConsultingLLC.com/
rab@GaslightSoftware.com http://GaslightSoftware.com/

The problem with this is that he doesn’t want to find Payments where testing = true, he wants to find Payments where testing = value of testing attribute for the User instance.

I think I got it:

class User < ActiveRecord::Base
  has_many :payments
  has_many :payments_with_testing,
           :class_name => 'Payment',
           :conditions => 'testing = \'#{self.testing}\''
end

and then I get this when running a test:

user_1.inspect
#<User id: 1, first_name: "Tom", last_name: "Smith", user_name:
"tom_smith", testing: "alfa", created_at: "2010-12-15 21:26:23",
updated_at: "2010-12-15 21:26:23">
user_1.payments (p.id, p.user_id, p.testing)
[[1, 1, "alfa"], [2, 1, "alfa"], [3, 1, "beta"]]
user_1.payments_with_testing (p.id, p.user_id, p.testing)
[[1, 1, "alfa"], [2, 1, "alfa"]] ### only the associated records with
testin = 'alfa' are returned
user_2.inspect
#<User id: 2, first_name: "Chris", last_name: "Stone", user_name:
"chris_stone", testing: "gamma", created_at: "2010-12-15 21:26:23",
updated_at: "2010-12-15 21:26:23">
user_2.payments (p.id, p.user_id, p.testing)
[[4, 2, "alfa"], [5, 2, "gamma"]]
user_2.payments_with_testing (p.id, p.user_id, p.testing)
[[5, 2, "gamma"]]

  User Load (0.2ms) SELECT "users".* FROM "users" WHERE ("users"."id"
= 1) LIMIT 1
  Payment Load (0.2ms) SELECT "payments".* FROM "payments" WHERE
("payments".user_id = 1)
  Payment Load (0.2ms) SELECT "payments".* FROM "payments" WHERE
("payments".user_id = 1 AND (testing = 'alfa'))
  User Load (0.1ms) SELECT "users".* FROM "users" WHERE ("users"."id"
= 2) LIMIT 1
  Payment Load (0.1ms) SELECT "payments".* FROM "payments" WHERE
("payments".user_id = 2)
  Payment Load (0.1ms) SELECT "payments".* FROM "payments" WHERE
("payments".user_id = 2 AND (testing = 'gamma'))

This relies on the trick that the condition is only evaluated later in
the proces
when quoted with single quotes.
(ref: http://www.dweebd.com/ruby/has_many-with-arguments/).

I would hope this could be done cleaner with a scope with a lambda,
but I didn't get this to work straight away.

HTH,

Peter

PS. I used this migration to create the payments table:

class CreatePayments < ActiveRecord::Migration
  def self.up
    create_table :payments do |t|
      t.references :user, :null => false
      t.string :testing
      t.timestamps
    end
  end

  def self.down
    drop_table :payments
  end
end

Hi all,
Thanks for your replies.

I want to get related models like this.

@payments=@user.payment
When @user.testing == true then return @user.payments=(payment.testing
== true)
When @user.testing == false then return
@user.payments=(payment.testing == false)

Therefore "where(:testing => true)" is not suitable for this case.
The payment.testing value depends on user.testing value(same as
user.testing value).

Masuda

Tim

You're exactly right.

Thanks

Goro Kuroita wrote in post #968571:

Hi,
I'm developing with ruby-1.9. 1 rails 3.0.0.
I want to relate 'User' model and 'Payment' model with condition which
has the same value as 'user.testing' in 'payment.testing'

I did

Class User < ActiveRecord::Base
    has_many payments, :conditions => ['testing = ?', self.testing]
end

This doesn't work.
I tried also below, but doesn't work too.
has_many payments, :conditions => ['testing = ?',
#{self.send(:testing)}]

Maybe this could work:

class User < ActiveRecord::Base
has_many :payments
has_many :payments_with_testing,
          :class_name => 'Payment',
          :conditions => 'testing = \'#{self.testing}\''
end

and then I get this when running a test:

user_1.inspect
#<User id: 1, first_name: "Tom", last_name: "Smith", user_name:
"tom_smith", testing: "alfa", created_at: "2010-12-15 21:26:23",
updated_at: "2010-12-15 21:26:23">
# print user_1.payments [p.id, p.user_id, p.testing]
[[1, 1, "alfa"], [2, 1, "alfa"], [3, 1, "beta"]]
# print user_1.payments_with_testing [p.id, p.user_id, p.testing]
[[1, 1, "alfa"], [2, 1, "alfa"]] ### only the associated records with
testing = 'alfa' are returned

user_2.inspect
#<User id: 2, first_name: "Chris", last_name: "Stone", user_name:
"chris_stone", testing: "gamma", created_at: "2010-12-15 21:26:23",
updated_at: "2010-12-15 21:26:23">
# print user_2.payments [p.id, p.user_id, p.testing]
[[4, 2, "alfa"], [5, 2, "gamma"]]
# print user_2.payments_with_testing [p.id, p.user_id, p.testing]
[[5, 2, "gamma"]]

User Load (0.2ms) SELECT "users".* FROM "users" WHERE ("users"."id"
= 1) LIMIT 1
Payment Load (0.2ms) SELECT "payments".* FROM "payments" WHERE
("payments".user_id = 1)
Payment Load (0.2ms) SELECT "payments".* FROM "payments" WHERE
("payments".user_id = 1 AND (testing = 'alfa'))
User Load (0.1ms) SELECT "users".* FROM "users" WHERE ("users"."id"
= 2) LIMIT 1
Payment Load (0.1ms) SELECT "payments".* FROM "payments" WHERE
("payments".user_id = 2)
Payment Load (0.1ms) SELECT "payments".* FROM "payments" WHERE
("payments".user_id = 2 AND (testing = 'gamma'))

This relies on the trick that the condition is only evaluated later in
the proces when quoted with single quotes.
(ref: http://www.dweebd.com/ruby/has_many-with-arguments/).

I would hope this could be done cleaner with a scope with a lambda,
but I didn't get this to work straight away.

HTH,

Peter

PS. I used this migration to create the payments table:

class CreatePayments < ActiveRecord::Migration
def self.up
   create_table :payments do |t|
     t.references :user, :null => false
     t.string :testing
     t.timestamps
   end
end

def self.down
   drop_table :payments
end
end

Hi Peter.

Your code worked fine!
It helped me great.

And for all respondent
Thanks for your help.