Gogov
2010-Oct-18 16:46 UTC
How to make an ActiveRecord::Base object to have a list of strings?
Hi, I''m new to Rails and already did some stuff with ActiveRecord associations but I cannot do one thing: I''ve got a ==class Paragraph < ActiveRecord::Base end == And I want each Paragraph to contain a list of words (each word is simply a String). I want to have a table with all Paragraphs and save/ load them. And finally get a json rep of a paragraph. Simple. If I do ==class Paragraph < ActiveRecord::Base has_many :words end class Word < ActiveRecord::Base belongs_to :paragraph end # and put corresponding stuff in the db schema create_table "words", :force => true do |t| t.integer "paragraph_id" t.string "content" end == So, this would work, and a json rep of a paragraph would look like (stripped some stuff for clarity) =={"words": [{"content" : "Once"}, {"content" : "upon"}, {"content" : "a"}, {"content" : "time"}]} == but I don''t want this extra complexity so I want to get something like =={"words": ["Once", "upon", "a", "time"]} == A plain array of strings without the extra nesting. But I can''t do ==class Paragraph < ActiveRecord::Base has_many :words, :class_name => "String" end # without the Word class at all == I could add some additional to_json parameters I guess and tweak it to give me what I want but it''d be ugly. How do I achieve this with ActiveRecord? I guess a paragraphs_words join table would work but I can''t work out the details. Thanks, Martin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
pepe
2010-Oct-19 00:27 UTC
Re: How to make an ActiveRecord::Base object to have a list of strings?
This won''t answer your question but I hope it helps you because your approach sounds too complicated to me, unless it is just for playing and learning. Why would you want to store each word in a record in a table? What if a word belongs to more than one paragraph? On Oct 18, 12:46 pm, Gogov <mgo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > I''m new to Rails and already did some stuff with ActiveRecord > associations but I cannot do one thing: > > I''ve got a > ==> class Paragraph < ActiveRecord::Base > end > ==> > And I want each Paragraph to contain a list of words (each word is > simply a String). I want to have a table with all Paragraphs and save/ > load them. And finally get a json rep of a paragraph. Simple. If I do > ==> class Paragraph < ActiveRecord::Base > has_many :words > end > > class Word < ActiveRecord::Base > belongs_to :paragraph > end > > # and put corresponding stuff in the db schema > > create_table "words", :force => true do |t| > t.integer "paragraph_id" > t.string "content" > end > ==> > So, this would work, and a json rep of a paragraph would look like > (stripped some stuff for clarity) > ==> {"words": [{"content" : "Once"}, {"content" : "upon"}, {"content" : > "a"}, {"content" : "time"}]} > ==> > but I don''t want this extra complexity so I want to get something like > ==> {"words": ["Once", "upon", "a", "time"]} > ==> > A plain array of strings without the extra nesting. > > But I can''t do > ==> class Paragraph < ActiveRecord::Base > has_many :words, :class_name => "String" > end > > # without the Word class at all > ==> > I could add some additional to_json parameters I guess and tweak it to > give me what I want but it''d be ugly. > How do I achieve this with ActiveRecord? I guess a paragraphs_words > join table would work but I can''t work out the details. > > Thanks, > Martin-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Erol Fornoles
2010-Oct-19 00:41 UTC
Re: How to make an ActiveRecord::Base object to have a list of strings?
How about hiding the "ugly" code on a to_simplified_json method in your Paragraph model? On Tue, Oct 19, 2010 at 12:46 AM, Gogov <mgogov-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > I''m new to Rails and already did some stuff with ActiveRecord > associations but I cannot do one thing: > > I''ve got a > ==> class Paragraph < ActiveRecord::Base > end > ==> > And I want each Paragraph to contain a list of words (each word is > simply a String). I want to have a table with all Paragraphs and save/ > load them. And finally get a json rep of a paragraph. Simple. If I do > ==> class Paragraph < ActiveRecord::Base > has_many :words > end > > class Word < ActiveRecord::Base > belongs_to :paragraph > end > > # and put corresponding stuff in the db schema > > create_table "words", :force => true do |t| > t.integer "paragraph_id" > t.string "content" > end > ==> > So, this would work, and a json rep of a paragraph would look like > (stripped some stuff for clarity) > ==> {"words": [{"content" : "Once"}, {"content" : "upon"}, {"content" : > "a"}, {"content" : "time"}]} > ==> > but I don''t want this extra complexity so I want to get something like > ==> {"words": ["Once", "upon", "a", "time"]} > ==> > A plain array of strings without the extra nesting. > > But I can''t do > ==> class Paragraph < ActiveRecord::Base > has_many :words, :class_name => "String" > end > > # without the Word class at all > ==> > I could add some additional to_json parameters I guess and tweak it to > give me what I want but it''d be ugly. > How do I achieve this with ActiveRecord? I guess a paragraphs_words > join table would work but I can''t work out the details. > > Thanks, > Martin > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- Erol M. Fornoles http://erolfornoles.posterous.com http://github.com/Erol http://twitter.com/erolfornoles http://ph.linkedin.com/in/erolfornoles -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Gogov
2010-Oct-19 08:53 UTC
Re: How to make an ActiveRecord::Base object to have a list of strings?
@pepe: Yeah, one word could belong to many paragraphs but it was actually more for experimentation purposes. With Hibernate in Java one could use annotations like @Embedded to achieve something similar so I wanted to compare Hibernate and ActiveRecord but in reality, you''re right, it is a bit too complicated (: @Erol Yep, I guess I''ll end up doing just that (: Just wondered if there is something neater to do. Thank you, Martin On Oct 19, 3:41 am, Erol Fornoles <erol.forno...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> How about hiding the "ugly" code on a to_simplified_json method in your > Paragraph model? > > > > > > On Tue, Oct 19, 2010 at 12:46 AM, Gogov <mgo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Hi, > > > I''m new to Rails and already did some stuff with ActiveRecord > > associations but I cannot do one thing: > > > I''ve got a > > ==> > class Paragraph < ActiveRecord::Base > > end > > ==> > > And I want each Paragraph to contain a list of words (each word is > > simply a String). I want to have a table with all Paragraphs and save/ > > load them. And finally get a json rep of a paragraph. Simple. If I do > > ==> > class Paragraph < ActiveRecord::Base > > has_many :words > > end > > > class Word < ActiveRecord::Base > > belongs_to :paragraph > > end > > > # and put corresponding stuff in the db schema > > > create_table "words", :force => true do |t| > > t.integer "paragraph_id" > > t.string "content" > > end > > ==> > > So, this would work, and a json rep of a paragraph would look like > > (stripped some stuff for clarity) > > ==> > {"words": [{"content" : "Once"}, {"content" : "upon"}, {"content" : > > "a"}, {"content" : "time"}]} > > ==> > > but I don''t want this extra complexity so I want to get something like > > ==> > {"words": ["Once", "upon", "a", "time"]} > > ==> > > A plain array of strings without the extra nesting. > > > But I can''t do > > ==> > class Paragraph < ActiveRecord::Base > > has_many :words, :class_name => "String" > > end > > > # without the Word class at all > > ==> > > I could add some additional to_json parameters I guess and tweak it to > > give me what I want but it''d be ugly. > > How do I achieve this with ActiveRecord? I guess a paragraphs_words > > join table would work but I can''t work out the details. > > > Thanks, > > Martin > > > -- > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscrib e@googlegroups.com> > > . > > For more options, visit this group at > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > -- > Erol M. Fornoleshttp://erolfornoles.posterous.comhttp://github.com/Erolhttp://twitter.com/erolfornoleshttp://ph.linkedin.com/in/erolfornoles-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2010-Oct-19 10:07 UTC
Re: How to make an ActiveRecord::Base object to have a list of strings?
On Oct 18, 5:46 pm, Gogov <mgo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> ==> class Paragraph < ActiveRecord::Base > has_many :words, :class_name => "String" > endis serialize :words, Array what you are looking for ? Fred -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Gogov
2010-Oct-23 09:54 UTC
Re: How to make an ActiveRecord::Base object to have a list of strings?
I guess I can use that. Thank you! Martin On Oct 19, 1:07 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Oct 18, 5:46 pm, Gogov <mgo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > ==> > class Paragraph < ActiveRecord::Base > > has_many :words, :class_name => "String" > > end > > is > > serialize :words, Array > > what you are looking for ? > > Fred-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Matias Fierro
2010-Oct-25 00:23 UTC
Re: How to make an ActiveRecord::Base object to have a list of strings?
Serialize ? What do that ? On Oct 23, 6:54 am, Gogov <mgo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I guess I can use that. Thank you! > > Martin > > On Oct 19, 1:07 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > > > > > > > On Oct 18, 5:46 pm, Gogov <mgo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > ==> > > class Paragraph < ActiveRecord::Base > > > has_many :words, :class_name => "String" > > > end > > > is > > > serialize :words, Array > > > what you are looking for ? > > > Fred-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2010-Oct-26 16:17 UTC
Re: How to make an ActiveRecord::Base object to have a list of strings?
Matias Fierro wrote in post #956786:> Serialize ? What do that ?It does exactly what the docs say it does. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.