Hi, I just figured out how to use rails new pagination support in more complex cases and since I wanted to share this I used this as an opportunity to start yet-another-rails-related-blog. So, you can read how I solved my problem and some discussion about possible improvements here. http://www.kanthak.net/explorations/blog/rails/exploring_paginations.html Hope it is useful to some of you and not totally trivial... Sebastian
On Mar 30, 2005 10:33 AM, Sebastian Kanthak <sebastian.kanthak-ZS8b95Whz3sUSW6y5lq3GQ@public.gmane.org> wrote:> I just figured out how to use rails new pagination support in more > complex cases and since I wanted to share this I used this as an > opportunity to start yet-another-rails-related-blog.Note that you also need to create your custom count method to support count_by_foo, where foo is part of the join that you are doing. You reference that specialized count method, but don''t discuss the issue. It bothers me that you basically have to cut''n''paste from find_by_sql to make this work. Violates DRY but I haven''t yet found an elegant workaround. Arien
Arien Malec wrote:>On Mar 30, 2005 10:33 AM, Sebastian Kanthak ><sebastian.kanthak-ZS8b95Whz3sUSW6y5lq3GQ@public.gmane.org> wrote: > > >>I just figured out how to use rails new pagination support in more >>complex cases and since I wanted to share this I used this as an >>opportunity to start yet-another-rails-related-blog. >> >> > >Note that you also need to create your custom count method to support >count_by_foo, where foo is part of the join that you are doing. You >reference that specialized count method, but don''t discuss the issue. > > >you''re absolutely right, here. I just didn''t include the source code because it''s fairly obvious and the post is already pretty long, but I should have mentioned the DRY issue.>It bothers me that you basically have to cut''n''paste from find_by_sql >to make this work. Violates DRY but I haven''t yet found an elegant >workaround. > >That bothered me as well. Notice my suggestion at the end of the article. If all the necessary information could be passed to the "paginator" method (the only things missing are an option for piggy-back fields and for query parametes, I think), the Paginator class could pass these parameters on to AR::Base.find_all and AR:Base.count (they have to be added here as well) and we would be DRY-compliant again, I believe. Sebastian
On Mar 31, 2005 11:15 AM, Sebastian Kanthak <sebastian.kanthak-ZS8b95Whz3sUSW6y5lq3GQ@public.gmane.org> wrote:> >It bothers me that you basically have to cut''n''paste from find_by_sql > >to make this work. Violates DRY but I haven''t yet found an elegant > >workaround. > That bothered me as well. Notice my suggestion at the end of the > article. If all the necessary information could be passed to the > "paginator" method (the only things missing are an option for piggy-back > fields and for query parametes, I think), the Paginator class could pass > these parameters on to AR::Base.find_all and AR:Base.count (they have to > be added here as well) and we would be DRY-compliant again, I believe.Belongs as a class method in AR::Base, IMHO Imagine: Foo < ActiveRecord::Base piggyback: :bar => [:baz,:quux] end Then :find_all and :count automatically join in the baz and quux fields from the bars table. Gets a bit tricky in that you have to support non-standard foreign keys. Arien
Arien Malec wrote:>Imagine: > >Foo < ActiveRecord::Base > piggyback: :bar => [:baz,:quux] >end > >Then :find_all and :count automatically join in the baz and quux >fields from the bars table. > >I have to admit that looks pretty slick. Way easier than forcing people to write custom SQL queries. On the other hand, it means you always get these attributes and not only when you need them. If we already go this way, we could even go a step further: Foo < ActiveRecord::Base belongs_to :bar, :eager => true end so that the association is always fetched eagerly. I guess "piggyback" wouldn''t let you do anything more, right? And "foo.bar.baz" is certainly cleaner than "the piggyback foo.baz". Sebastian
On Mar 31, 2005 11:45 AM, Arien Malec <arien.malec-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: [automatic piggybacking]> Belongs as a class method in AR::Base, IMHO > > Imagine: > > Foo < ActiveRecord::Base > piggyback: :bar => [:baz,:quux] > endOn reflection, I think this should be: Foo < ActiveRecord::Base belongs_to :bar, :include => [:baz, :quux] end I''ve looked at the source, but I''m not up at the moment to putting together a prototype -- I need to understand how Rails is put together. There''s some block and tackling stuff in the join logic (handling multiple belongs_to relationships, handling non-standard foreign keys). How are updates to piggybacked records currently handled? Arien
> How are updates to piggybacked records currently handled?I don''t handle piggybacked record updating any differently. If I have to make some extra calls, then fine. Updates aren''t occuring as much as page views, so I don''t view it as a problem. Also, my models are simple enough that I never have to make more than 1 extra call anyhow. -- rick http://techno-weenie.net
On Apr 1, 2005 9:21 AM, Rick Olson <technoweenie-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > How are updates to piggybacked records currently handled? > > I don''t handle piggybacked record updating any differently. If I have > to make some extra calls, then fine. Updates aren''t occuring as much > as page views, so I don''t view it as a problem.I mean, if I do: b = Foo.find_by_id(1) b.baz = "blah" b.save Where Foo.find_all does a join between foos and bars, does Rails update the bars table? Arien
How to get offline Rails'' API manual ? I really need it ... Thanks in advance. -- Best regards, Arie _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails