I''d like to be able to add some find_by_sql-based find methods to a has_many association in one of my models. Is there a good way to do that, or should I just create extra methods in the model, rather than in the association? (I don''t know if I explained that right. Basically, what I''d like to be able to do is create a new thing.widgets.special_optimized_find method, where widgets is a has_many collection in the things object.) Tyler
The thing about has_many and HABTM is that they don''t return objects, they return an array of objects. If your model called "Project" has a has_many :people, then Project.people is a property, not a method. All has_many does is run a pre-defined ActiveRecord::Base.Find() and append the results (an array of whatever type objects) to your object. So if you were trying to do `project.people.method`, it''s not going to work. Likewise, I haven''t yet figured out how to call methods I''ve written from a child object. Like: I can make Project.this_is_my_method() work, but I''ve never gotten p = project; p.this_is_my_method() to work. So perhaps this is a limitation of Active Record? Or is there some workaround I''m not aware of? - DD On 7/18/05, Tyler Kiley <tyler.kiley-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''d like to be able to add some find_by_sql-based find methods to a > has_many association in one of my models. Is there a good way to do > that, or should I just create extra methods in the model, rather than > in the association? > > (I don''t know if I explained that right. Basically, what I''d like to > be able to do is create a new thing.widgets.special_optimized_find > method, where widgets is a has_many collection in the things object.) > > Tyler > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- === === === === === === === === === === === ==ddemaree-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://practicalmadness.com/
For the first part, maybe you''d be interested in testing this patch: http://dev.rubyonrails.com/ticket/1764 As for the second part, it depends on how you define your methods. Example: Project.something: class Project < ... def self.something ... end end p = Project.new p.something: class Project def something ... end end (notice the "self." in the method name or not; one is a class method and one is an instance method) -d. On 7/18/05, David Demaree <ddemaree-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> The thing about has_many and HABTM is that they don''t return objects, > they return an array of objects. If your model called "Project" has a > has_many :people, then Project.people is a property, not a method. All > has_many does is run a pre-defined ActiveRecord::Base.Find() and > append the results (an array of whatever type objects) to your object. > So if you were trying to do `project.people.method`, it''s not going to > work. > > Likewise, I haven''t yet figured out how to call methods I''ve written > from a child object. Like: I can make Project.this_is_my_method() > work, but I''ve never gotten p = project; p.this_is_my_method() to > work.
Ah, got it. Thanks! - DD On 7/18/05, Dan Peterson <dpiddy-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> For the first part, maybe you''d be interested in testing this patch: > > http://dev.rubyonrails.com/ticket/1764 > > As for the second part, it depends on how you define your methods. Example: > > Project.something: > > class Project < ... > def self.something > ... > end > end > > p = Project.new > p.something: > > class Project > def something > ... > end > end > > (notice the "self." in the method name or not; one is a class method > and one is an instance method) > > -d. > > On 7/18/05, David Demaree <ddemaree-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > The thing about has_many and HABTM is that they don''t return objects, > > they return an array of objects. If your model called "Project" has a > > has_many :people, then Project.people is a property, not a method. All > > has_many does is run a pre-defined ActiveRecord::Base.Find() and > > append the results (an array of whatever type objects) to your object. > > So if you were trying to do `project.people.method`, it''s not going to > > work. > > > > Likewise, I haven''t yet figured out how to call methods I''ve written > > from a child object. Like: I can make Project.this_is_my_method() > > work, but I''ve never gotten p = project; p.this_is_my_method() to > > work. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- === === === === === === === === === === === ==ddemaree-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://practicalmadness.com/