Hi, is it possible to define a column in a model, which is not present in the table? this column should return a combination of two other columns. Model.columnC=Model.columnA+"-"+Model.columnB A and B are in the table, C isn''t I was thinking of attr_accessor, but i cant get it to work any pointers? -- 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 -~----------~----~----~----~------~----~------~--~---
Remco Hh wrote:> is it possible to define a column in a model, which is not present in > the table? this column should return a combination of two other columns. > > Model.columnC=Model.columnA+"-"+Model.columnB > > A and B are in the table, C isn''tIf you just want to grab this value, you just need a standard Ruby method in your model definition: def columnC columnA + "-" + columnB end If you want to be able to write to this attribute as well, then you''ll have to write a method along the lines of def columnC=(new_value) columnA, columnB = new_value.split("-") new_value end although obviously there is some ambiguity with "strings-like-this". Hope that helps, Chris --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> is it possible to define a column in a model, which is not present in > the table? this column should return a combination of two other > columns. > > Model.columnC=Model.columnA+"-"+Model.columnB > > A and B are in the table, C isn''t > > I was thinking of attr_accessor, but i cant get it to work > > any pointers?You only need to use attr_accessor if you''re looking to set the value as well as get it. You should just be able to create a method to do this: class Cheese < ActiveRecord::Base def name origin+" "+type end end my_cheese = Cheese.new my_cheese.origin = "Kent" my_cheese.type = "Stilton" puts my_cheese.name -> "Kent Stilton" Hope that helps, Steve --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Have a look at ActiveRecord''s composed_of thingy Fred -- 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 -~----------~----~----~----~------~----~------~--~---
No, that doesn''t help this virtual column needs to be included in the dataset when i do a find:all query Stephen Bartholomew wrote:>> any pointers? > You only need to use attr_accessor if you''re looking to set the value > as well as get it. You should just be able to create a method to do > this: > > class Cheese < ActiveRecord::Base > def name > origin+" "+type > end > end > > my_cheese = Cheese.new > my_cheese.origin = "Kent" > my_cheese.type = "Stilton" > puts my_cheese.name -> "Kent Stilton" > > Hope that helps, > > Steve-- 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 -~----------~----~----~----~------~----~------~--~---
There might be some misunderstaning here, at lest on my part - The dataset is a group of objects. When you do: model_instance.name -- you''re not really accessing any data, you''re calling a method inside ActiveRecord, (method_missing, actually) If you had manually defined a method called ''name'', there would be no difference to the code using the model. It just sees a method. Unless by saying ''the dataset'', you mean the model''s @attributes variable? Cheers Starr --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
If performing a find all is your goal with the results being the join of two tables fields why not just find all for each field and use .join to combine their results in a new array. Then just use .each to iterate through that array and/ar access it by its indexes? Perhaps a little bit exhaustive for your goal but it is an option. Im sure there is something more DRY though. On 10/9/06, Starr <snhorne-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > There might be some misunderstaning here, at lest on my part - > > The dataset is a group of objects. When you do: model_instance.name -- > you''re not really accessing any data, you''re calling a method inside > ActiveRecord, (method_missing, actually) > > If you had manually defined a method called ''name'', there would be no > difference to the code using the model. It just sees a method. > > Unless by saying ''the dataset'', you mean the model''s @attributes > variable? > > Cheers > Starr > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 10/9/06, Benjamin Gorlick <uncledrails-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > If performing a find all is your goal with the results being the join of > two tables fields why not just find all for each field and use .join to > combine their results in a new array. Then just use .each to iterate through > that array and/ar access it by its indexes? Perhaps a little bit exhaustive > for your goal but it is an option. Im sure there is something more DRY > though.Or write a new find method named FindWithVirtualColumn or some such, that writes the actual SQL query you''re trying to emulate. ActiveRecord is great, but don''t be afraid of SQL when neccessary. On 10/9/06, Starr <snhorne-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > > > > There might be some misunderstaning here, at lest on my part - > > > > The dataset is a group of objects. When you do: model_instance.name -- > > you''re not really accessing any data, you''re calling a method inside > > ActiveRecord, (method_missing, actually) > > > > If you had manually defined a method called ''name'', there would be no > > difference to the code using the model. It just sees a method. > > > > Unless by saying ''the dataset'', you mean the model''s @attributes > > variable? > > > > Cheers > > Starr > > > > > > > > > >-- ===Tanner Burson==tanner.burson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://tannerburson.com <---Might even work one day... --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---