Hi everyone! I''m new to ror, and i''m stuck with what might be kind of a beginners problem. Here''s a brief description of what i''m trying to do: I have two classes, lets say Class_a and Class_b, with their models "having" several instances of Class_c (via has_many). As far as i understand it, i have to put those lines in the model of Class_c: belongs_to :Class_a belongs_to :Class_b When im trying to save an instance of Class_c (right after saving say Class_b), im getting an "SQLite3::SQLException: SQL logic error or missing database: INSERT INTO Class_c"-Error. Can this be caused by the fact that there is noch Class_a_id? To make my point clear: Class_c should belong to Class_a OR Class_b. While saving an instance of Class_c i only have either an instance of Class_a OR Class_b to refer to. Is it possible to have many belongs_to relationships in one model? I''m not seeing how to implement the "c belongs to a OR b", perhaps someone can lighten things up for me. Thanks in advance, JC -- 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 -~----------~----~----~----~------~----~------~--~---
On Sat, Feb 28, 2009 at 5:16 AM, Joe Canares < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Hi everyone! > > I''m new to ror, and i''m stuck with what might be kind of a beginners > problem. > > Here''s a brief description of what i''m trying to do: > > I have two classes, lets say Class_a and Class_b, with their models > "having" several instances of Class_c (via has_many). As far as i > understand it, i have to put those lines in the model of Class_c: > > belongs_to :Class_a > belongs_to :Class_b > > When im trying to save an instance of Class_c (right after saving say > Class_b), im getting an "SQLite3::SQLException: SQL logic error or > missing database: INSERT INTO Class_c"-Error. Can this be caused by the > fact that there is noch Class_a_id? > > To make my point clear: Class_c should belong to Class_a OR Class_b. > While saving an instance of Class_c i only have either an instance of > Class_a OR Class_b to refer to. > > Is it possible to have many belongs_to relationships in one model? I''m > not seeing how to implement the "c belongs to a OR b", perhaps someone > can lighten things up for me. > > Thanks in advance, > JCHi, Class_* do not represent valid model names in Rails in the default case. In any case, I would recommend reading AWDwR 3rd Edition if you''re new to Rails. Good luck, -Conrad --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Conrad Taylor wrote:> On Sat, Feb 28, 2009 at 5:16 AM, Joe Canares < > rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi, Class_* do not represent valid model names in Rails in the default > case.Thanks for the reply, Actually i picked bad names for my example, lets say my classes are named classa, classb and classc. In classa-model i have: has_many :classcs classb-model: has_many :classcs classc-model: belongs_to :classa belongs_to :classb -- 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 -~----------~----~----~----~------~----~------~--~---
On Sat, Feb 28, 2009 at 5:51 AM, Joe Canares < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Conrad Taylor wrote: > > On Sat, Feb 28, 2009 at 5:16 AM, Joe Canares < > > rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > > > Hi, Class_* do not represent valid model names in Rails in the default > > case. > Thanks for the reply, > > Actually i picked bad names for my example, lets say my classes are > named classa, classb and classc. > > In classa-model i have: > > has_many :classcs > > classb-model: > > has_many :classcs > > classc-model: > > belongs_to :classa > belongs_to :classb >Hi, in the original e-mail you said that you were receiving an error message in regards to SQLite3. OK, if you have classa and classb, you should have created the following databases: classas classbs If this isn''t the case, you''ll need to create the databases. -Conrad --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Conrad Taylor wrote:> Hi, in the original e-mail you said that you were receiving an error > message in regards to > SQLite3. OK, if you have classa and classb, you should have created the > following > databases: > > classas > classbs > > If this isn''t the case, you''ll need to create the databases.I am sure you meant tables, not databases ... :-D --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Conrad Taylor wrote:> On Sat, Feb 28, 2009 at 5:51 AM, Joe Canares < > rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi, in the original e-mail you said that you were receiving an error > message > in regards to > SQLite3. OK, if you have classa and classb, you should have created the > following > databases: > > classas > classbsMy database contains the tables classas, classcs and classcs, with classcs having the colums classa_id and classb_id. Saving an instance of classa with its classc works fine, but saving classb doesn''t. I''m not sure if it is even possible to have two belongs_to-relationship in a model, because that would imply that one of the foreign key colums will be NULL after saving. That was actually my question in the first place, sorry if my example confused everybody =) JC -- 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 -~----------~----~----~----~------~----~------~--~---
On Sat, Feb 28, 2009 at 6:04 AM, Martin Eisenhardt < martin.eisenhardt-nq6vx7WXLM5AfugRpC6u6w@public.gmane.org> wrote:> > Conrad Taylor wrote: > > > Hi, in the original e-mail you said that you were receiving an error > > message in regards to > > SQLite3. OK, if you have classa and classb, you should have created the > > following > > databases: > > > > classas > > classbs > > > > If this isn''t the case, you''ll need to create the databases. > > > I am sure you meant tables, not databases ... :-D >Martin, thanks for correcting me.> > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Sat, Feb 28, 2009 at 6:06 AM, Joe Canares < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Conrad Taylor wrote: > > On Sat, Feb 28, 2009 at 5:51 AM, Joe Canares < > > rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > > > Hi, in the original e-mail you said that you were receiving an error > > message > > in regards to > > SQLite3. OK, if you have classa and classb, you should have created the > > following > > databases: > > > > classas > > classbs > > > My database contains the tables classas, classcs and classcs, with > classcs having the colums classa_id and classb_id. Saving an instance of > classa with its classc works fine, but saving classb doesn''t. > > I''m not sure if it is even possible to have two belongs_to-relationship > in a model, because that would imply that one of the foreign key colums > will be NULL after saving. That was actually my question in the first > place, sorry if my example confused everybody =) > > JCHi, please explain what you''re trying to do. It''s rare that you''ll have two belongs_to declaration. However, I''m sure this may be possible in some use cases. -Conrad --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Conrad Taylor wrote:> On Sat, Feb 28, 2009 at 6:06 AM, Joe Canares < > rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> Hi, please explain what you''re trying to do. It''s rare that you''ll have > two > belongs_to declaration. However, I''m sure this may be possible in some > use cases.I''m trying to append files to either a "post" or a "message". Files belong to either a post or a message. So post-model: has_many :files message-model: has_many :files file-model: belongs_to :post belongs_to :message I''m trying to add the file-functionality to the existing post/message-structure. -- 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 -~----------~----~----~----~------~----~------~--~---
On Feb 28, 2:44 pm, Joe Canares <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > I''m trying to append files to either a "post" or a "message". Files > belong to either a post or a message. > > So post-model: > > has_many :files > > message-model: > > has_many :files > > file-model: > > belongs_to :post > belongs_to :message > > I''m trying to add the file-functionality to the existing > post/message-structure.You might want to have a look at polymorphic associations, or you could just have two separate belongs_to, one of which will be null for any given file. Fred> -- > 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On Feb 28, 2:44�pm, Joe Canares <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote:> you > could just have two separate belongs_to, one of which will be null for > any given file.Thank you Fred, thats actually what i wanted to know. Since the SQLException isn''t very explicit i was wondering if having many belongs_to is even possible. At least now i know where not to look for my error. ;) Have a nice weekend everyone! JC -- 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
What you want is polymorphic joins. Look it up in google. Blog: http://random8.zenunit.com/ Learn rails: http://sensei.zenunit.com/ On 01/03/2009, at 1:06 AM, Joe Canares <rails-mailing-list@andreas- s.net> wrote:> > Conrad Taylor wrote: >> On Sat, Feb 28, 2009 at 5:51 AM, Joe Canares < >> rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > >> Hi, in the original e-mail you said that you were receiving an error >> message >> in regards to >> SQLite3. OK, if you have classa and classb, you should have >> created the >> following >> databases: >> >> classas >> classbs > > > My database contains the tables classas, classcs and classcs, with > classcs having the colums classa_id and classb_id. Saving an > instance of > classa with its classc works fine, but saving classb doesn''t. > > I''m not sure if it is even possible to have two belongs_to- > relationship > in a model, because that would imply that one of the foreign key > colums > will be NULL after saving. That was actually my question in the first > place, sorry if my example confused everybody =) > > JC > > -- > 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 -~----------~----~----~----~------~----~------~--~---