Forum: Ruby on Rails Two belongs_to relationships in one model?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E79fbfb38220422744751817564fae72?d=identicon&s=25 Joe Canares (joe_canares)
on 2009-02-28 14:16
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
40db9e75b3f5899258e3bdc0c9210154?d=identicon&s=25 Conrad Taylor (conradwt)
on 2009-02-28 14:46
(Received via mailing list)
On Sat, Feb 28, 2009 at 5:16 AM, Joe Canares <
rails-mailing-list@andreas-s.net> wrote:

> understand it, i have to put those lines in the model of Class_c:
> 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


Hi, 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
E79fbfb38220422744751817564fae72?d=identicon&s=25 Joe Canares (joe_canares)
on 2009-02-28 14:51
Conrad Taylor wrote:
> On Sat, Feb 28, 2009 at 5:16 AM, Joe Canares <
> rails-mailing-list@andreas-s.net> 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
40db9e75b3f5899258e3bdc0c9210154?d=identicon&s=25 Conrad Taylor (conradwt)
on 2009-02-28 14:59
(Received via mailing list)
On Sat, Feb 28, 2009 at 5:51 AM, Joe Canares <
rails-mailing-list@andreas-s.net> wrote:

> named classa, classb and classc.
>
> 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
Cee2bfe6b28e92e8ff5507cde0736cad?d=identicon&s=25 Martin Eisenhardt (Guest)
on 2009-02-28 15:05
(Received via mailing list)
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
E79fbfb38220422744751817564fae72?d=identicon&s=25 Joe Canares (joe_canares)
on 2009-02-28 15:06
Conrad Taylor wrote:
> On Sat, Feb 28, 2009 at 5:51 AM, Joe Canares <
> rails-mailing-list@andreas-s.net> 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
40db9e75b3f5899258e3bdc0c9210154?d=identicon&s=25 Conrad Taylor (conradwt)
on 2009-02-28 15:14
(Received via mailing list)
On Sat, Feb 28, 2009 at 6:04 AM, Martin Eisenhardt <
martin.eisenhardt@mneisen.org> wrote:

> > 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.
40db9e75b3f5899258e3bdc0c9210154?d=identicon&s=25 Conrad Taylor (conradwt)
on 2009-02-28 15:23
(Received via mailing list)
On Sat, Feb 28, 2009 at 6:06 AM, Joe Canares <
rails-mailing-list@andreas-s.net> wrote:

> > databases:
> 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


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.

-Conrad
E79fbfb38220422744751817564fae72?d=identicon&s=25 Joe Canares (joe_canares)
on 2009-02-28 15:44
Conrad Taylor wrote:
> On Sat, Feb 28, 2009 at 6:06 AM, Joe Canares <
> rails-mailing-list@andreas-s.net> 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.
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-02-28 15:52
(Received via mailing list)
On Feb 28, 2:44 pm, Joe Canares <rails-mailing-l...@andreas-s.net>
wrote:
> 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
E79fbfb38220422744751817564fae72?d=identicon&s=25 Joe Canares (joe_canares)
on 2009-02-28 15:56
Frederick Cheung wrote:
> On Feb 28, 2:44�pm, Joe Canares <rails-mailing-l...@andreas-s.net>
> 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
3131fcea0a711e5ad89c8d49cc9253b4?d=identicon&s=25 Julian Leviston (Guest)
on 2009-02-28 20:07
(Received via mailing list)
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-
This topic is locked and can not be replied to.