Hi: Tables, as you know, are meant to have a primary key called "id". Having joined two tables using the "find(:all, :joins) method, I find that my joined result set has two columns called id, one for each table. Unfortunately, when I try to pass table A.id as the parameter for editing I am getting the table B.id sent over. How can I avoid including the "id" of the second table? bruce
> Having joined two tables using the "find(:all, :joins) method, I find > that my joined result set has two columns called id, one for each > table. Unfortunately, when I try to pass table A.id as the parameter > for editing I am getting the table B.id sent over. > > How can I avoid including the "id" of the second table?Specify which columns you want using :select as in find(:select => "a.*, b.piggy, b.back" ... ) -- David Heinemeier Hansson http://www.loudthinking.com -- Broadcasting Brain http://www.basecamphq.com -- Online project management http://www.backpackit.com -- Personal information manager http://www.rubyonrails.com -- Web-application framework
Wow! Firstly, I apologise for having asked a question with such an obvious answer. I looked everywhere and did not find that option. Secondly, Thank you for answering my question. Thirdly, I am incredibly impressed that you (David) are taking the time to answer our questions. I very much appreciate it. Fourthly, I am in love with Ruby on Rails. I am ex PHP guy. This framework is a masterpiece. And even though you may have suspected as much, I hope it is nice for you to know that one more person is impressed. I am overwhelmed its beauty. Finally, Once I am good enough, I shall return this favour you have done me by answering questions for others. You got me, I''m in the community now. Bruce Balmer PS. I used to be an international speaker (I decided to change careers), I also am a former world-record holder in speed learning. I''d be interested to help the ruby / ruby on rails movement by applying my speaking and training skills. If that info has value for you, keep me on file and let me know when it suits you to talk about that. On 5-Nov-05, at 3:14 PM, David Heinemeier Hansson wrote:>> Having joined two tables using the "find(:all, :joins) method, I find >> that my joined result set has two columns called id, one for each >> table. Unfortunately, when I try to pass table A.id as the parameter >> for editing I am getting the table B.id sent over. >> >> How can I avoid including the "id" of the second table? >> > > Specify which columns you want using :select as in find(:select => > "a.*, b.piggy, b.back" ... ) > -- > David Heinemeier Hansson > http://www.loudthinking.com -- Broadcasting Brain > http://www.basecamphq.com -- Online project management > http://www.backpackit.com -- Personal information manager > http://www.rubyonrails.com -- Web-application framework > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Hi: Can anyone point me to a document of best practices? Here is my situation. I can think of 3 solutions, but which is best? There may be more solutions I have not considered, so feel free to let me know those also. I have a line like this @x = model.find(:all, :conditions=>blah, blah, blah) (which will lead to a small number of records, typically less than 100) I wish to calculate the total of @x.time. A. I could do that by making another call to the db and asking it to give me the total and place that value in a new variable then use that variable in my rhtml file. That would keep calculations where they belong - off the presentation layer. But it makes another call to the db. B. I could use a for loop and run through the values in @x.time totalling them as I go. That would avoid a hit to the db but it would involve running through the same array twice, once on the controller and once in the rhtml page where I am displaying a table row for each element of the array. So this has duplicated effort. C. I could just do the totalling while using the "for" loop on the rhtml page (the one that makes one row of a table for each row from the db. This saves a double hit to the db or a double looping through the array but seems to clutter up the rhtml file with things (calculations) that should not be there. What "best practice" would experienced rails programmers suggest? bruce _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
in controller: @x = X.find(:all, ...) @x_time_total = @x.inject(0) { |total, x| sum + x.time } this is the cleanest approach in my opinion. On 11/26/05, Bruce Balmer <brucebalmer-ee4meeAH724@public.gmane.org> wrote:> > Hi: > Can anyone point me to a document of best practices? Here is my > situation. I can think of 3 solutions, but which is best? There may be more > solutions I have not considered, so feel free to let me know those also. > > > I have a line like this > > @x = model.find(:all, :conditions=>blah, blah, blah) (which will lead to > a small number of records, typically less than 100) > > I wish to calculate the total of @x.time. > > *A. I could do that by making another call to the db and asking it to give > me the total and place that value in a new variable then use that variable > in my rhtml file. That would keep calculations where they belong - off the > presentation layer. But it makes another call to the db.* > > *B. *I could use a for loop and run through the values in @x.timetotalling them as I go. That would avoid a hit to the db but it would > involve running through the same array twice, once on the controller and > once in the rhtml page where I am displaying a table row for each element of > the array. So this has duplicated effort. > > *C. I could just do the totalling while using the "for" loop on the rhtml > page (the one that makes one row of a table for each row from the db. This > saves a double hit to the db or a double looping through the array but seems > to clutter up the rhtml file with things (calculations) that should not be > there.* > > What "best practice" would experienced rails programmers suggest? > > bruce > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Chris: Thank you. bruce On 26-Nov-05, at 6:23 AM, Chris Hall wrote:> in controller: > > @x = X.find(:all, ...) > @x_time_total = @x.inject(0) { |total, x| sum + x.time } > > this is the cleanest approach in my opinion. > > On 11/26/05, Bruce Balmer <brucebalmer-ee4meeAH724@public.gmane.org> wrote: > Hi: > > Can anyone point me to a document of best practices? Here is my > situation. I can think of 3 solutions, but which is best? There may > be more solutions I have not considered, so feel free to let me > know those also. > > > I have a line like this > > @x = model.find(:all, :conditions=>blah, blah, blah) (which will > lead to a small number of records, typically less than 100) > > I wish to calculate the total of @x.time. > > A. I could do that by making another call to the db and asking it > to give me the total and place that value in a new variable then > use that variable in my rhtml file. That would keep calculations > where they belong - off the presentation layer. But it makes > another call to the db. > > B. I could use a for loop and run through the values in @x.time > totalling them as I go. That would avoid a hit to the db but it > would involve running through the same array twice, once on the > controller and once in the rhtml page where I am displaying a table > row for each element of the array. So this has duplicated effort. > > C. I could just do the totalling while using the "for" loop on the > rhtml page (the one that makes one row of a table for each row from > the db. This saves a double hit to the db or a double looping > through the array but seems to clutter up the rhtml file with > things (calculations) that should not be there. > > What "best practice" would experienced rails programmers suggest? > > bruce > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails