I have a table called Post (not the actual name) where users input various piece of information, Most of the input is done by dynamic select''s (drop downs). As an example: User would choose Field: Input: Values that go into Post table Category Full Time 2 Length 45 Days 4 State Colorado 7 Skill Rails 1 The values are derived from other tables, Static tables that would only be accessed via form fields. Since I don''t think there should or could be any real relationships between these "static" tables (only administrator can CRUD these tables) and the Post table, would the correct option for displaying results (list) and editing be doine via joins ? TIA Stuart -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060613/2c888f17/attachment.html
Is this a poorly constructed question ? Still trying to figure this one out. In PHP I would have used left joins, but it seems (from what I''ve read they are not popular in RoR. Stuart On 6/13/06, Dark Ambient <sambient@gmail.com> wrote:> > I have a table called Post (not the actual name) where users input various > piece of information, Most of the input is done by dynamic select''s (drop > downs). > As an example: > User would choose > Field: Input: Values that go > into Post table > Category Full Time 2 > Length 45 Days 4 > State Colorado 7 > Skill Rails 1 > > The values are derived from other tables, Static tables that would only be > accessed via form fields. > > Since I don''t think there should or could be any real relationships > between these "static" tables (only administrator can CRUD these tables) and > the Post table, would the correct option > for displaying results (list) and editing be doine via joins ? > > TIA > Stuart >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060614/702e7b35/attachment.html
I don''t understand this question either, can you provide a bit more information on what you''re trying to achieve? A bit off-topic:> In PHP I would have used left joins, but it seems (from what I''ve read > they > are not popular in RoR.Left Joins are used in Rails to help implement eager loading of associations (if I''m not mistaken). Where have you read they''re not popular in RoR? -- Posted via http://www.ruby-forum.com/.
Hopefully this will explain it better. I''ll break it down to a smaller example. I have 2 tables, "users" and "states" In the users table I have a field "state" (where the user would input their home state) However, the value for the state field in the user table is a number value, drawn from the id field of the corresponding state in the states table. The states table is static in the sense that it is never going to be written to or edited by users. users table user | state | -------|---------| 1 3 states table id | state | -----|---------------| 3 California So for display purposes, search forms, etc the state id (in this case 3 for California) would not be shown, only the label (California). So in some ways I can see doing a "has one, belongs to" but in the past I''ve used joins. In Agile Web Development , when the author mentions joins he also says something like, but you probably won''t be using these very much and references table relationships (HABTM, etc). Stuart On 6/14/06, Ryan Allen <ruby-forum@badfont.com> wrote:> > I don''t understand this question either, can you provide a bit more > information on what you''re trying to achieve? > > A bit off-topic: > > > In PHP I would have used left joins, but it seems (from what I''ve read > > they > > are not popular in RoR. > > Left Joins are used in Rails to help implement eager loading of > associations (if I''m not mistaken). Where have you read they''re not > popular in RoR? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060614/44e64dd3/attachment.html
Dark Ambient wrote:> Hopefully this will explain it better. > I''ll break it down to a smaller example. > > I have 2 tables, "users" and "states" > > In the users table I have a field "state" (where the user would input > their home state) > However, the value for the state field in the user table is a number > value, drawn from the id field of the corresponding state in the > states table. The states table is static in the sense that it is > never going to be written to or edited by users. > > users table > > user | state | > -------|---------| > 1 3 > > states table > > id | state | > -----|---------------| > 3 California > > So for display purposes, search forms, etc the state id (in this case > 3 for California) would not be shown, only the label (California). > So in some ways I can see doing a "has one, belongs to" but in the > past I''ve used joins. > In Agile Web Development , when the author mentions joins he also says > something like, but you probably won''t be using these very much and > references table relationships (HABTM, etc). > > StuartHi Stuart, Since I know that you used the "four days with rails" tutorial, can''t you use the method that is used for displaying the categories for the To-Do list. It sounds that your scenario is quite similar, is it not? The only difference as I see it is that the "states table" is static, as against the "categories table" in the four days example (which should make your life easier, I''d think). Of course, I''m still new to the concepts, so I hope I''m not annoying the "council elders" :) would love to know if there''s another way to do this easily. Cheers, Mohit.
Mohit, Thank you for the reply. Actually I have not done the "four days..." tutorial. I think today I will :). Yesterday I did the "Rolling with Rails", cookbook2 which btw I thought was very good. Rolling also uses a categories table which (I''m guessing) is similar to the one in Four days. In Rolling , categories can be edited, and added to. It has a field that associates recipes with a category , and the recipes table has a field for categories. I''m just not sure if it applies to my situation and while I will try it that way the real motivation for my question is "what''s the most practical way" and "what way makes the most sense in terms of say overhead and simplicity". Stuart On 6/14/06, Mohit Sindhwani <mo_mail@onghu.com> wrote:> > Dark Ambient wrote: > > Hopefully this will explain it better. > > I''ll break it down to a smaller example. > > > > I have 2 tables, "users" and "states" > > > > In the users table I have a field "state" (where the user would input > > their home state) > > However, the value for the state field in the user table is a number > > value, drawn from the id field of the corresponding state in the > > states table. The states table is static in the sense that it is > > never going to be written to or edited by users. > > > > users table > > > > user | state | > > -------|---------| > > 1 3 > > > > states table > > > > id | state | > > -----|---------------| > > 3 California > > > > So for display purposes, search forms, etc the state id (in this case > > 3 for California) would not be shown, only the label (California). > > So in some ways I can see doing a "has one, belongs to" but in the > > past I''ve used joins. > > In Agile Web Development , when the author mentions joins he also says > > something like, but you probably won''t be using these very much and > > references table relationships (HABTM, etc). > > > > Stuart > > Hi Stuart, > > Since I know that you used the "four days with rails" tutorial, can''t > you use the method that is used for displaying the categories for the > To-Do list. It sounds that your scenario is quite similar, is it not? > The only difference as I see it is that the "states table" is static, as > against the "categories table" in the four days example (which should > make your life easier, I''d think). > > Of course, I''m still new to the concepts, so I hope I''m not annoying the > "council elders" :) would love to know if there''s another way to do > this easily. > > Cheers, > Mohit. > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060614/9f8a7a91/attachment.html
On 6/14/06, Dark Ambient <sambient@gmail.com> wrote:> > Mohit, Thank you for the reply. Actually I have not done the "four > days..." tutorial. I think today I will :). > Yesterday I did the "Rolling with Rails", cookbook2 which btw I thought > was very good. Rolling also uses a categories table which (I''m guessing) is > similar to the one in Four days. In Rolling , categories can be edited, and > added to. It has a field that associates recipes with a category , and the > recipes table has a field for categories. I''m just not sure if it applies > to my situation and while I will try it that way the real motivation for my > question is "what''s the most practical way" and "what way makes the most > sense in terms of say overhead and simplicity". > > Stuart > > > On 6/14/06, Mohit Sindhwani <mo_mail@onghu.com> wrote: > > > > Dark Ambient wrote: > > > Hopefully this will explain it better. > > > I''ll break it down to a smaller example. > > > > > > I have 2 tables, "users" and "states" > > > > > > In the users table I have a field "state" (where the user would input > > > their home state) > > > However, the value for the state field in the user table is a number > > > value, drawn from the id field of the corresponding state in the > > > states table. The states table is static in the sense that it is > > > never going to be written to or edited by users. > > > > > > users table > > > > > > user | state | > > > -------|---------| > > > 1 3 > > > > > > states table > > > > > > id | state | > > > -----|---------------| > > > 3 California > > > > > > So for display purposes, search forms, etc the state id (in this case > > > 3 for California) would not be shown, only the label (California). > > > So in some ways I can see doing a "has one, belongs to" but in the > > > past I''ve used joins. > > > In Agile Web Development , when the author mentions joins he also says > > > something like, but you probably won''t be using these very much and > > > references table relationships (HABTM, etc). > > > > > > Stuart > > > > Hi Stuart, > > > > Since I know that you used the "four days with rails" tutorial, can''t > > you use the method that is used for displaying the categories for the > > To-Do list. It sounds that your scenario is quite similar, is it not? > > The only difference as I see it is that the "states table" is static, as > > against the "categories table" in the four days example (which should > > make your life easier, I''d think). > > > > Of course, I''m still new to the concepts, so I hope I''m not annoying the > > "council elders" :) would love to know if there''s another way to do > > this easily. > > > > Cheers, > > Mohit. > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060614/5554b3e2/attachment.html
You can use joins if you want to - but for your example, you wouldn''t need them. If you have: User - belongs_to :state Then you can just load the state directly: user = User.find(1) user.state.state -> "California" (Incidently, your ''state'' column in the ''users'' table should be ''state_id'') This will however, make 2 db calls; one to get the user, the next to load the state when you reference it. If you''re concerned about the performance in this situation, you can use eager loading: user = User.find(1, :include => [:state]) user.state.state -> "California" In this example, there is only one query; Rails will do a join to get the state on the first find query. If you *really* want to have your own joins, you can: user = User.find(1, :joins => "LEFT JOIN states ON users.state_id = states.id") However, the reason this is rarely used is because, firstly you don''t need it most of the time and it also means that the resulting records do not directly map to models i.e. the columns returned will be a combination of User and State. Hope that helps, Steve Dark Ambient wrote:> Hopefully this will explain it better. > I''ll break it down to a smaller example. > > I have 2 tables, "users" and "states" > > In the users table I have a field "state" (where the user would input > their home state) > However, the value for the state field in the user table is a number > value, drawn from the id field of the corresponding state in the states > table. The states table is static in the sense that it is never going > to be written to or edited by users. > > users table > > user | state | > -------|---------| > 1 3 > > states table > > id | state | > -----|---------------| > 3 California > > So for display purposes, search forms, etc the state id (in this case 3 > for California) would not be shown, only the label (California). > So in some ways I can see doing a "has one, belongs to" but in the past > I''ve used joins. > In Agile Web Development , when the author mentions joins he also says > something like, but you probably won''t be using these very much and > references table relationships (HABTM, etc). > > Stuart > > On 6/14/06, *Ryan Allen* <ruby-forum@badfont.com > <mailto:ruby-forum@badfont.com>> wrote: > > I don''t understand this question either, can you provide a bit more > information on what you''re trying to achieve? > > A bit off-topic: > > > In PHP I would have used left joins, but it seems (from what I''ve > read > > they > > are not popular in RoR. > > Left Joins are used in Rails to help implement eager loading of > associations (if I''m not mistaken). Where have you read they''re not > popular in RoR? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org <mailto:Rails@lists.rubyonrails.org> > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > ------------------------------------------------------------------------ > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > ------------------------------------------------------------------------ > > No virus found in this incoming message. > Checked by AVG Free Edition. > Version: 7.1.394 / Virus Database: 268.8.4/363 - Release Date: 13/06/2006
Steve, Yes, helps big time! Thank you. Stuart On 6/14/06, Stephen Bartholomew <steve@curve21.com> wrote:> > You can use joins if you want to - but for your example, you wouldn''t > need them. If you have: > > User - belongs_to :state > > Then you can just load the state directly: > user = User.find(1) > user.state.state -> "California" > > (Incidently, your ''state'' column in the ''users'' table should be > ''state_id'') > > This will however, make 2 db calls; one to get the user, the next to > load the state when you reference it. > > If you''re concerned about the performance in this situation, you can use > eager loading: > > user = User.find(1, :include => [:state]) > user.state.state -> "California" > > In this example, there is only one query; Rails will do a join to get > the state on the first find query. > > If you *really* want to have your own joins, you can: > user = User.find(1, :joins => "LEFT JOIN states ON users.state_id > states.id") > > However, the reason this is rarely used is because, firstly you don''t > need it most of the time and it also means that the resulting records do > not directly map to models i.e. the columns returned will be a > combination of User and State. > > Hope that helps, > Steve > > Dark Ambient wrote: > > Hopefully this will explain it better. > > I''ll break it down to a smaller example. > > > > I have 2 tables, "users" and "states" > > > > In the users table I have a field "state" (where the user would input > > their home state) > > However, the value for the state field in the user table is a number > > value, drawn from the id field of the corresponding state in the states > > table. The states table is static in the sense that it is never going > > to be written to or edited by users. > > > > users table > > > > user | state | > > -------|---------| > > 1 3 > > > > states table > > > > id | state | > > -----|---------------| > > 3 California > > > > So for display purposes, search forms, etc the state id (in this case 3 > > for California) would not be shown, only the label (California). > > So in some ways I can see doing a "has one, belongs to" but in the past > > I''ve used joins. > > In Agile Web Development , when the author mentions joins he also says > > something like, but you probably won''t be using these very much and > > references table relationships (HABTM, etc). > > > > Stuart > > > > On 6/14/06, *Ryan Allen* <ruby-forum@badfont.com > > <mailto:ruby-forum@badfont.com>> wrote: > > > > I don''t understand this question either, can you provide a bit more > > information on what you''re trying to achieve? > > > > A bit off-topic: > > > > > In PHP I would have used left joins, but it seems (from what I''ve > > read > > > they > > > are not popular in RoR. > > > > Left Joins are used in Rails to help implement eager loading of > > associations (if I''m not mistaken). Where have you read they''re not > > popular in RoR? > > > > -- > > Posted via http://www.ruby-forum.com/. > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org <mailto:Rails@lists.rubyonrails.org> > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > ------------------------------------------------------------------------ > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > ------------------------------------------------------------------------ > > > > No virus found in this incoming message. > > Checked by AVG Free Edition. > > Version: 7.1.394 / Virus Database: 268.8.4/363 - Release Date: > 13/06/2006 > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060614/5e663e3f/attachment-0001.html