Hi, maybe this is a well known issue, btw it wasn''t to me :-) I''ve quickly checked the mailing list without . I''ve got thoose two models Riba and Fatture class Riba < ActiveRecord::Base set_table_name :riba belongs_to :fatture class Fatture < ActiveRecord::Base set_table_name :fatture belongs_to :client belongs_to :tipo_pagamento has_many :bolle has_many :righe_fatture has_many :riba within Riba I''ve got a method looking for a riba list #list_riba = self.find(:all, # :conditions => ["extract(month from datafattura) = ? AND extract(year from datafattura) = ?", mese_scelto, anno], # :joins => "INNER JOIN fatture ON fatture.id = riba.fatture_id", # :order => "fatture.numero") list_riba = self.find_by_sql(["SELECT r.id, fatture_id, importo, stampata FROM riba r INNER JOIN fatture ON fatture.id = r.fatture_id WHERE extract(month from datafattura) = ? AND extract(year from datafattura) = ? ORDER BY fatture.numero", mese_scelto, anno]) using the commented way, when I refer to list_riba[i].id I get the value of list_riba.fatture.id Using the second way list_riba[i].id actually refer to the correct Riba.id value Is something about the way I''ve done it? Or is it just a lack of the find method? Enrico -- "The only thing necessary for the triumph of evil is for good men to do nothing" Edmund Burke
Enrico Teotti <agenteo@...> writes:> Hi, > maybe this is a well known issue, btw it wasn''t to me > I''ve quickly checked the mailing list without .> Is something about the way I''ve done it? Or is it just a lack of the > find method?I''ve been bitten by this too and found the solution; http://dev.rubyonrails.com/ticket/1338 I don''t think it has been implemented in 0.13.1 yet but you can put something like: class ActiveRecord::Base def self.construct_finder_sql(options) joins = options[:joins] order = options[:order] select = options[:select] || ''*'' sql = "SELECT #{select} FROM #{table_name} " sql << "#{joins} " if joins add_conditions!(sql, options[:conditions]) sql << "ORDER BY #{order} " if order add_limit!(sql, options) sql end end in your enviroment.rb file as a temporary workaround. With this in place you can include :select => ''riba.*'' in your find statement to select the proper fields; list_riba = self.find(:all, :select => ''riba.*'', :conditions => ["extract(month from datafattura) = ? AND extract(year from datafattura) = ?", mese_scelto, anno], :joins => "INNER JOIN fatture ON fatture.id = riba.fatture_id", :order => "fatture.numero") HTH, Remco
Remco van t Veer <rwvtveer@...> writes:> class ActiveRecord::Base > def self.construct_finder_sql(options)Should be: class << ActiveRecord::Base def construct_finder_sql(options) Cheers, Remco
Mark Reginald James
2005-Oct-05 02:52 UTC
Re: false id using ActiveRecord find :all :joins
Remco van t Veer wrote:> Remco van t Veer <rwvtveer@...> writes: > >> class ActiveRecord::Base >> def self.construct_finder_sql(options) > > > Should be: > > class << ActiveRecord::Base > def construct_finder_sql(options)Remco, would you be able to explain the difference between these. And also in what cases do you instead have to use module_eval/class_eval to modify AR methods via user libraries and environment.rb. -- We develop, watch us RoR, in numbers too big to ignore.
Mark Reginald James <mrj@...> writes:> >> class ActiveRecord::Base > >> def self.construct_finder_sql(options) > > > > Should be: > > > > class << ActiveRecord::Base > > def construct_finder_sql(options) > > Remco, would you be able to explain the difference > between these. And also in what cases do you instead > have to use module_eval/class_eval to modify AR > methods via user libraries and environment.rb.The first snippet is just wrong. It redefines AR::Base and leaves you with a AR::Base with nothing but the given method. I was too busy when I was writing that post and only noticed it when I was checking for a reply. The new snippet changes the definition of the AR::Base class _object_. It is only possible to alter class methods in this way. Since I only need to change a class method here the "class <<" method is fine. The class_eval method is much more powerful, it also allows you the change object methods and other stuff. HTH, Remco
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Oct 4, 2005, at 11:44 PM, Remco van t Veer wrote:> Mark Reginald James <mrj@...> writes: >>>> class ActiveRecord::Base >>>> def self.construct_finder_sql(options) >>> >>> Should be: >>> >>> class << ActiveRecord::Base >>> def construct_finder_sql(options) >>> >> >> Remco, would you be able to explain the difference >> between these. And also in what cases do you instead >> have to use module_eval/class_eval to modify AR >> methods via user libraries and environment.rb. >> > > The first snippet is just wrong. It redefines AR::Base and leaves > you with a > AR::Base with nothing but the given method. I was too busy when I > was writing > that post and only noticed it when I was checking for a reply.Ruby classes are open. The first way is fine. Fire up irb and play around with it. Best, jeremy -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (Darwin) iD8DBQFDQ3kEAQHALep9HFYRAhbKAJ9WE4bxJmkA3LI/C9KRo1pp1d6QRQCgjTme um6eHNFJ8HCGLMMikCUNJNY=4LjZ -----END PGP SIGNATURE-----
Jeremy Kemper <jeremy@...> writes:> On Oct 4, 2005, at 11:44 PM, Remco van t Veer wrote: > > Mark Reginald James <mrj <at> ...> writes: > >>>> class ActiveRecord::Base > >>>> def self.construct_finder_sql(options) > >>> > >>> Should be: > >>> > >>> class << ActiveRecord::Base > >>> def construct_finder_sql(options) > > > > The first snippet is just wrong. It redefines AR::Base and leaves > > you with a > > AR::Base with nothing but the given method. I was too busy when I > > was writing > > that post and only noticed it when I was checking for a reply. > > Ruby classes are open. The first way is fine. Fire up irb and play > around with it.Wow.. The java programmer in me is very angry now! ;) Apparently there''s a lot of ways to change a class definition. After reading; http://www.rubygarden.org/ruby?ClassMethodsTutorial I will change my enviroment.rb to: def ActiveRecord::Base.construct_finder_sql(options) .. end Thanks Jeremy for your hint. I need to do my meta programming katas. Remco