On Wed, Dec 15, 2010 at 3:24 PM, Masuda <msd....@gmail.com> wrote:
> 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?

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

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to