Two belongs_to relationships in one model?

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

On Sat, Feb 28, 2009 at 5:16 AM, Joe C. <
[email protected]> 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

Conrad T. wrote:

On Sat, Feb 28, 2009 at 5:16 AM, Joe C. <
[email protected]> 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

Conrad T. 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 … :smiley:

On Sat, Feb 28, 2009 at 5:51 AM, Joe C. <
[email protected]> 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

Conrad T. wrote:

On Sat, Feb 28, 2009 at 5:51 AM, Joe C. <
[email protected]> 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

On Sat, Feb 28, 2009 at 6:04 AM, Martin E. <
[email protected]> wrote:

classbs

If this isn’t the case, you’ll need to create the databases.

I am sure you meant tables, not databases … :smiley:

Martin, thanks for correcting me.

On Sat, Feb 28, 2009 at 6:06 AM, Joe C. <
[email protected]> 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

On Feb 28, 2:44 pm, Joe C. [email protected]
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

Conrad T. wrote:

On Sat, Feb 28, 2009 at 6:06 AM, Joe C. <
[email protected]> 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.

Frederick C. wrote:

On Feb 28, 2:44�pm, Joe C. [email protected]
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. :wink:

Have a nice weekend everyone!

JC

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 C. <rails-mailing-list@andreas-