Kevin Alons
2007-Jul-18 03:58 UTC
Add database field expression (such as Person::Fullname)
I am new to Rails development, having done c++ and other database driven app development in the past. Often I would create a database view that would combine, for example, first and last name values into a "virtual" field called FullName. I don''t see how to use database views with Rails, so I am thinking that another way to this would be to define a virtual attribute on the appropriate class to do the same thing (the derived ActiveRecord or ApplicationController classes?), but I am unclear where and how this should be declared. Can anyone point me in the right direction? Kevin -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Rick Olson
2007-Jul-18 04:23 UTC
Re: Add database field expression (such as Person::Fullname)
On 7/17/07, Kevin Alons <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > I am new to Rails development, having done c++ and other database driven > app development in the past. Often I would create a database view that > would combine, for example, first and last name values into a "virtual" > field called FullName. > > I don''t see how to use database views with Rails, so I am thinking that > another way to this would be to define a virtual attribute on the > appropriate class to do the same thing (the derived ActiveRecord or > ApplicationController classes?), but I am unclear where and how this > should be declared. > > Can anyone point me in the right direction?ActiveRecord docs under "Overwriting default accessors" http://rails.rubyonrails.org/classes/ActiveRecord/Base.html -- Rick Olson http://lighthouseapp.com http://weblog.techno-weenie.net http://mephistoblog.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Matthew Rudy
2007-Jul-18 11:29 UTC
Re: Add database field expression (such as Person::Fullname)
I''m quite interested in the idea of views, but I''ve never used them, i imagine that if you create a view called "people_view", which is a view on to the people table, then ActiveRecord will just see this as a normal table? so class PersonView < ActiveRecord::Base set_table_name :people_view end will work. otherwise, just do it in ruby class Person < ActiveRecord::Base def full_name [first_name, last_name].compact.join(" ") end end is the normal thing to do. Kevin Alons wrote:> I am new to Rails development, having done c++ and other database driven > app development in the past. Often I would create a database view that > would combine, for example, first and last name values into a "virtual" > field called FullName. > > I don''t see how to use database views with Rails, so I am thinking that > another way to this would be to define a virtual attribute on the > appropriate class to do the same thing (the derived ActiveRecord or > ApplicationController classes?), but I am unclear where and how this > should be declared. > > Can anyone point me in the right direction? > > Kevin-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thorsten
2007-Jul-18 12:25 UTC
Re: Add database field expression (such as Person::Fullname)
@Mathew Rudy: Views have nothing to do with the database tables. Views are just HTML Templates with embedded Ruby to display the data. so your first idea is totally the wrong direction ;) However, your 2nd way is the way to go. On 18 Jul., 13:29, Matthew Rudy <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I''m quite interested in the idea of views, > but I''ve never used them, > > i imagine that if you create a view called "people_view", > which is a view on to the people table, > > then ActiveRecord will just see this as a normal table? > so > > class PersonView < ActiveRecord::Base > set_table_name :people_view > end > > will work. > > otherwise, > just do it in ruby > > class Person < ActiveRecord::Base > def full_name > [first_name, last_name].compact.join(" ") > end > end > > is the normal thing to do. > > Kevin Alons wrote: > > I am new to Rails development, having done c++ and other database driven > > app development in the past. Often I would create a database view that > > would combine, for example, first and last name values into a "virtual" > > field called FullName. > > > I don''t see how to use database views with Rails, so I am thinking that > > another way to this would be to define a virtual attribute on the > > appropriate class to do the same thing (the derived ActiveRecord or > > ApplicationController classes?), but I am unclear where and how this > > should be declared. > > > Can anyone point me in the right direction? > > > Kevin > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Mikkel Bruun
2007-Jul-18 13:27 UTC
Re: Add database field expression (such as Person::Fullname)
Thorsten wrote:> @Mathew Rudy: Views have nothing to do with the database tables. Views > are just HTML Templates with embedded Ruby to display the data. > so your first idea is totally the wrong direction ;) > > However, your 2nd way is the way to go. > > On 18 Jul., 13:29, Matthew Rudy <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>Thorsten the original poster was asking about database views, not html templates... -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thorsten
2007-Jul-18 13:34 UTC
Re: Add database field expression (such as Person::Fullname)
Sorry, my mistake :-P On 18 Jul., 15:27, Mikkel Bruun <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Thorsten wrote: > > @Mathew Rudy: Views have nothing to do with the database tables. Views > > are just HTML Templates with embedded Ruby to display the data. > > so your first idea is totally the wrong direction ;) > > > However, your 2nd way is the way to go. > > > On 18 Jul., 13:29, Matthew Rudy <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > Thorsten the original poster was asking about database views, not html > templates... > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Lionel Bouton
2007-Jul-18 13:48 UTC
Re: Add database field expression (such as Person::Fullname)
Kevin Alons wrote the following on 18.07.2007 05:58 :> I am new to Rails development, having done c++ and other database driven > app development in the past. Often I would create a database view that > would combine, for example, first and last name values into a "virtual" > field called FullName. > > I don''t see how to use database views with Rails, so I am thinking that > another way to this would be to define a virtual attribute on the > appropriate class to do the same thing (the derived ActiveRecord or > ApplicationController classes?), but I am unclear where and how this > should be declared. > > Can anyone point me in the right direction? >For this particular need, a simple ''full_name'' method in your model is probably simpler. If you need to affect names by full name, you can code a full_name= method too. It''s probably better because: - much simpler to handle, all your logic is in one place: the model, - Ruby is more powerful than pure SQL (ie: your full_name will be easier to code and extend), - doing it in the model instead of the DB is more flexible, a model has access to code (libraries) you can share easily with other parts of your application (most likely other models), - you don''t have to aggregate data accross rows or join tables to get at this data, so doing it in the model can''t be a problem for performance (which is not to say that you''ll get better perf using views when joins are implicated), in fact accessing the DB only once to instanciate the model will yield better performance in your case. Lionel --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Kevin Alons
2007-Jul-18 13:51 UTC
Re: Add database field expression (such as Person::Fullname)
I want to say that this is a most helpful forum! The availability of quick, knowledgeable and polite assistance is wonderful! I have been skeptical about Ruby on Rails as a development language, but I may already be won over... Thanks again for your help (I already have more questions!). -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Chris Hall
2007-Jul-18 14:28 UTC
Re: Add database field expression (such as Person::Fullname)
class Person < AR::Base def fullname first_name + " " + lastname end end p = Person.create(:first_name => "Joe", :last_name => "Smith") p.first_name # "Joe" p.last_name # "Smith" p.fullname # "Joe Smith" On 7/17/07, Kevin Alons <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > I am new to Rails development, having done c++ and other database driven > app development in the past. Often I would create a database view that > would combine, for example, first and last name values into a "virtual" > field called FullName. > > I don''t see how to use database views with Rails, so I am thinking that > another way to this would be to define a virtual attribute on the > appropriate class to do the same thing (the derived ActiveRecord or > ApplicationController classes?), but I am unclear where and how this > should be declared. > > Can anyone point me in the right direction? > > Kevin > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Brian Hogan
2007-Jul-18 14:46 UTC
Re: Add database field expression (such as Person::Fullname)
I actually prefer def fullname "#{self.first_name} #{self.lastname}" end because I don''t get exceptions if someone leaves first_name or last_name nil. (That and I like scoping my vars, though that''s not always necessary.) On 7/18/07, Chris Hall <christopher.k.hall-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > class Person < AR::Base > > def fullname > first_name + " " + lastname > end > end > > p = Person.create(:first_name => "Joe", :last_name => "Smith") > p.first_name # "Joe" > p.last_name # "Smith" > p.fullname # "Joe Smith" > > > On 7/17/07, Kevin Alons <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > > > > I am new to Rails development, having done c++ and other database driven > > app development in the past. Often I would create a database view that > > would combine, for example, first and last name values into a "virtual" > > field called FullName. > > > > I don''t see how to use database views with Rails, so I am thinking that > > another way to this would be to define a virtual attribute on the > > appropriate class to do the same thing (the derived ActiveRecord or > > ApplicationController classes?), but I am unclear where and how this > > should be declared. > > > > Can anyone point me in the right direction? > > > > Kevin > > > > -- > > Posted via http://www.ruby-forum.com/. > > > > > > > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Lionel Bouton
2007-Jul-18 14:55 UTC
Re: Add database field expression (such as Person::Fullname)
Brian Hogan wrote the following on 18.07.2007 16:46 :> I actually prefer > > def fullname > "#{self.first_name} #{self.lastname}" > endWhen I said ''extend'' I had one use-case in mind, here''s a part of one librairy I coded: def fullname if firstname.blank? && lastname.blank? return _(''Unknown (fill your firstname and lastname in)'') elsif firstname.blank? || lastname.blank? # no need for space, one is empty return "#{firstname}#{lastname}" else return "#{firstname} #{lastname}" end end Notes: - _() is from ruby-gettext, - the .blank? are Rails specific IIRC, - the return are superfluous, I''m in the process of ditching some of them where they don''t help readability. This would be quite difficult to do in a SQL view... Lionel. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Matthew Rudy
2007-Jul-18 15:15 UTC
Re: Add database field expression (such as Person::Fullname)
I prefer mine; def full_name [first_name, last_name].compact.join(" ") end as it doesn''t include any unnecessary whitespace in the event that one of the names is not defined (or, indeed, both) Brian Hogan wrote:> I actually prefer > > def fullname > "#{self.first_name} #{self.lastname}" > end > > because I don''t get exceptions if someone leaves first_name or last_name > nil. (That and I like scoping my vars, though that''s not always > necessary.)-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Michael Glaesemann
2007-Jul-18 23:59 UTC
Re: Add database field expression (such as Person::Fullname)
On Jul 18, 2007, at 9:55 , Lionel Bouton wrote:> > Brian Hogan wrote the following on 18.07.2007 16:46 : >> I actually prefer >> >> def fullname >> "#{self.first_name} #{self.lastname}" >> end> - _() is from ruby-gettext,I''m surprised that in a localized app you''re using attributes such as "firstname" and "lastname" :)> This would be quite difficult to do in a SQL view...SELECT CASE WHEN firstname = '''' THEN lastname WHEN lastname = '''' THEN firstname ELSE firstname || '' '' || lastname END AS fullname FROM ... But I agree it''s easy to handle in the app, and especially for localizations. Michael Glaesemann grzm seespotcode net --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Lionel Bouton
2007-Jul-19 00:57 UTC
Re: Add database field expression (such as Person::Fullname)
Michael Glaesemann wrote the following on 19.07.2007 01:59 :> >> - _() is from ruby-gettext, >> > > I''m surprised that in a localized app you''re using attributes such as > "firstname" and "lastname" :) > >I was unaware of cultures where people are named by other means, care to point me to them? Lionel. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Michael Glaesemann
2007-Jul-19 01:21 UTC
Re: Add database field expression (such as Person::Fullname)
On Jul 18, 2007, at 19:57 , Lionel Bouton wrote:> > Michael Glaesemann wrote the following on 19.07.2007 01:59 : >> >>> - _() is from ruby-gettext, >>> >> >> I''m surprised that in a localized app you''re using attributes such as >> "firstname" and "lastname" :) >> >> > > I was unaware of cultures where people are named by other means, > care to > point me to them?Well, for example, Yukihiro Matsumoto would have his name order reversed in Japanese. In that case, what is *first* depends on the localization. Michael Glaesemann grzm seespotcode net --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Lionel Bouton
2007-Jul-19 02:49 UTC
Re: Add database field expression (such as Person::Fullname)
Michael Glaesemann wrote the following on 19.07.2007 03:21 :> Well, for example, Yukihiro Matsumoto would have his name order > reversed in Japanese. In that case, what is *first* depends on the > localization. >Ok. It seems fortunate for me that I''m still far from adressing the Japanese market. The problem is probably a vocabulary one (english isn''t my mother tongue) is forname/surname more suited? It seems a migration is not far away... Lionel. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Michael Glaesemann
2007-Jul-19 03:01 UTC
Re: Add database field expression (such as Person::Fullname)
On Jul 18, 2007, at 21:49 , Lionel Bouton wrote:> > Michael Glaesemann wrote the following on 19.07.2007 03:21 : >> Well, for example, Yukihiro Matsumoto would have his name order >> reversed in Japanese. In that case, what is *first* depends on the >> localization. >> > > Ok. It seems fortunate for me that I''m still far from adressing the > Japanese market. > The problem is probably a vocabulary one (english isn''t my mother > tongue) is forname/surname more suited?Depends on how complicated you want to make it :) I tend to go with given name, family name myself, but that still doesn''t cover all the variations of names around the world. Though I like surname, the "fore" in forename also carries the meaning of in front, which has the same issues as first name. But like I said, even these don''t cover the full range of human naming schemes. Michael Glaesemann grzm seespotcode net --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Brian Hogan
2007-Jul-19 12:59 UTC
Re: Add database field expression (such as Person::Fullname)
I love threads like these :) On 7/18/07, Michael Glaesemann <grzm-RYEyMNgfJnVLeUupdtUFmg@public.gmane.org> wrote:> > > > On Jul 18, 2007, at 21:49 , Lionel Bouton wrote: > > > > > Michael Glaesemann wrote the following on 19.07.2007 03:21 : > >> Well, for example, Yukihiro Matsumoto would have his name order > >> reversed in Japanese. In that case, what is *first* depends on the > >> localization. > >> > > > > Ok. It seems fortunate for me that I''m still far from adressing the > > Japanese market. > > The problem is probably a vocabulary one (english isn''t my mother > > tongue) is forname/surname more suited? > > Depends on how complicated you want to make it :) I tend to go with > given name, family name myself, but that still doesn''t cover all the > variations of names around the world. Though I like surname, the > "fore" in forename also carries the meaning of in front, which has > the same issues as first name. But like I said, even these don''t > cover the full range of human naming schemes. > > Michael Glaesemann > grzm seespotcode net > > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---