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 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-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
You could just use a method instead... class User < ActiveRecord::Base has_many :payments def testing_payments payments.where(:testing => testing) 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-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 15 December 2010 17:01, Tim Shaffer <timshaffer-BUHhN+a2lJ4@public.gmane.org> wrote:> You could just use a method instead... > > class User < ActiveRecord::Base > > has_many :payments > > def testing_payments > payments.where(:testing => testing) > endThat would be even better as a named scope. Colin -- 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-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Dec 15, 2010, at 12:04 PM, Colin Law wrote:> On 15 December 2010 17:01, Tim Shaffer <timshaffer-BUHhN+a2lJ4@public.gmane.org> wrote: >> You could just use a method instead... >> >> class User < ActiveRecord::Base >> >> has_many :payments >> >> def testing_payments >> payments.where(:testing => testing) >> end > > That would be even better as a named scope. > > ColinActually, 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-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org http://AgileConsultingLLC.com/ rab-/VpnD74mH8+00s0LW7PaslaTQe2KTcn/@public.gmane.org http://GaslightSoftware.com/ -- 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-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Wednesday, December 15, 2010 12:50:03 PM UTC-5, rob wrote:> > On Dec 15, 2010, at 12:04 PM, Colin Law wrote: > On 15 December 2010 17:01, > Tim Shaffer wrote: >> You could just use a method instead... >> >> class > User < ActiveRecord::Base >> >> has_many :payments >> >> def > testing_payments >> payments.where(:testing => testing) >> 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 R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org > http://AgileConsultingLLC.com/ r...-/VpnD74mH8+00s0LW7PaslaTQe2KTcn/@public.gmane.org > 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. -- 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-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Wed, Dec 15, 2010 at 3:24 PM, Masuda <msd.456-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 -- 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-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
>TimYou''re exactly right. Thanks -- 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-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 -- Posted via http://www.ruby-forum.com/. -- 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-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi Peter. Your code worked fine! It helped me great. And for all respondent Thanks for your help. On 12月16日, 午前6:30, Peter Vandenabeele <pe...@vandenabeele.com> wrote:> On Wed, Dec 15, 2010 at 3:24 PM, Masuda <msd....-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.