Hi I''m new to rails and I want to create an object named element with can hold sevral properties names and values. for example : e = Element.create() e.add(:name => "x", :value => 800) e.add(:name => "y", :value => 600) e.add(:name => "whatever", :value => "anything") e.get(:name => "x") #-> 800 e.get(:name => "y") #-> 300 e.get(:name => "whatever") #-> anything i was thinkings add the object var which holds (id, element_id, name, value) with a relationship of element has_many vars. how do i create the getter ? (e.get(:name => "x") and the setter ? thanks. anyone used this plugin http://svn.nkryptic.com/ ? -- 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 -~----------~----~----~----~------~----~------~--~---
Gady,
This is a perfect area where Ruby can work its magic. Here''s an
example of what you could do:
class Element < ActiveRecord::Base
has_many :variables
def [](name)
variables.find_by_name(name)
end
def []=(name,value)
var = variables.find_or_create_by_name(name)
var.value = value
var.save
end
end
class Variable < ActiveRecord::Base
belongs_to :element
end
Now you will be able to do the following:
e = Element.create
e["x"] = 800
e["y"] = 600
e["whatever"] = "anything"
e["x"] #-> "800"
e["y"] #-> "600"
e["whatever"] #-> "anything"
Isn''t that fantastic? Note that this is assuming that your value field
is a TEXT which will return anything as a string. You could also store
a type and coerce the values, let me know if you want more detail.
Michael Bleigh
michael-+08J6pdAJjhWk0Htik3J/w@public.gmane.org
On Dec 13, 2007, at 7:38 AM, Gady Sujo wrote:
>
> Hi
> I''m new to rails and I want to create an object named element with
can
> hold sevral properties names and values.
> for example :
>
> e = Element.create()
> e.add(:name => "x", :value => 800)
> e.add(:name => "y", :value => 600)
> e.add(:name => "whatever", :value => "anything")
>
>
> e.get(:name => "x") #-> 800
> e.get(:name => "y") #-> 300
> e.get(:name => "whatever") #-> anything
>
> i was thinkings add the object var which holds (id, element_id, name,
> value)
> with a relationship of element has_many vars.
> how do i create the getter ? (e.get(:name => "x")
> and the setter ?
>
> thanks.
>
> anyone used this plugin http://svn.nkryptic.com/ ?
> --
> 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
-~----------~----~----~----~------~----~------~--~---
Thanks allot.
never thought it can be that easy...
there is a small bug. it need to be
def [](name)
variables.find_by_name(name).value
end
Michael Bleigh wrote:> Gady,
>
> This is a perfect area where Ruby can work its magic. Here''s an
> example of what you could do:
>
> class Element < ActiveRecord::Base
> has_many :variables
>
> def [](name)
> variables.find_by_name(name)
> end
--
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
-~----------~----~----~----~------~----~------~--~---
Hi Gady,
Another (much lighter) way of doing this is with the
''serliaze''declaration, that serializes hashes or arrays
automagically.
class Element < ActiveRecord::Base
serialize :variables
end
Then you can just do setters with:
element.variables = {:price => ''4'', :availability =>
''7 Days''}
element.save
And the getter becomes:
element.variables[:price]
This way, you don''t need additional classes, tables or relationships.
Matt
On Dec 13, 10:38 pm, Gady Sujo
<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:> Hi
> I''m new to rails and I want to create an object named element with
can
> hold sevral properties names and values.
> for example :
>
> e = Element.create()
> e.add(:name => "x", :value => 800)
> e.add(:name => "y", :value => 600)
> e.add(:name => "whatever", :value => "anything")
>
> e.get(:name => "x") #-> 800
> e.get(:name => "y") #-> 300
> e.get(:name => "whatever") #-> anything
>
> i was thinkings add the object var which holds (id, element_id, name,
> value)
> with a relationship of element has_many vars.
> how do i create the getter ? (e.get(:name => "x")
> and the setter ?
>
> thanks.
>
> anyone used this pluginhttp://svn.nkryptic.com/?
> --
> 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
-~----------~----~----~----~------~----~------~--~---
Sorry, forgot to mention that ''variables'' must be a text attribute on Element. On Dec 14, 10:37 pm, Matthew Tonkin <m...-IwivumJW2WkWQnjQ7V0W7w@public.gmane.org> wrote:> Hi Gady, > > Another (much lighter) way of doing this is with the > ''serliaze''declaration, that serializes hashes or arrays automagically. > > class Element < ActiveRecord::Base > serialize :variables > end > > Then you can just do setters with: > > element.variables = {:price => ''4'', :availability => ''7 Days''} > element.save > > And the getter becomes: > element.variables[:price] > > This way, you don''t need additional classes, tables or relationships. > > Matt > > On Dec 13, 10:38 pm, Gady Sujo <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > > > Hi > > I''m new to rails and I want to create an object named element with can > > hold sevral properties names and values. > > for example : > > > e = Element.create() > > e.add(:name => "x", :value => 800) > > e.add(:name => "y", :value => 600) > > e.add(:name => "whatever", :value => "anything") > > > e.get(:name => "x") #-> 800 > > e.get(:name => "y") #-> 300 > > e.get(:name => "whatever") #-> anything > > > i was thinkings add the object var which holds (id, element_id, name, > > value) > > with a relationship of element has_many vars. > > how do i create the getter ? (e.get(:name => "x") > > and the setter ? > > > thanks. > > > anyone used this pluginhttp://svn.nkryptic.com/? > > -- > > 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 -~----------~----~----~----~------~----~------~--~---