Confused on how to associate models

I have Ruser and Bag models.
I’m using Ruser instead of User because I know “user” create problems
with a postgresql database.
Bags must be delivered to Ruser so I need to know when one or more
bags are delivered.
I create these associations:

class Ruser < ActiveRecord::Base
has_many :deliveries
has_many :bags, :through => :deliveries

class Bag < ActiveRecord::Base
has_many :deliveries
has_many :rusers, :through => :deliveries

class Delivery < ActiveRecord::Base
belongs_to :ruser
belongs_to :bag

Ruser and Bag are already populated.
Delivery has delivered_at attribute.

Now I want delivery 3 bags to ruser.
I can do
bags=Bags.all
user=Ruser.find(params[:id])
delivery = user.deliveries.create(:delivered_at => Date.today).
But I can’t do delivery.bags << bags.
I think that could be some mistakes in my associations.

On Jan 24, 12:48pm, Mauro [email protected] wrote:

Now I want delivery 3 bags to ruser.
I can do
bags=Bags.all
user=Ruser.find(params[:id])
delivery = user.deliveries.create(:delivered_at => Date.today).
But I can’t do delivery.bags << bags.
I think that could be some mistakes in my associations.

You’ve written that deliver belongs_to bag (ie there is at most one
bag for a delivery). With the way your models currently exist, you’d
need to create one delivery for each bag.

Fred

On 24 January 2011 15:33, Marnen Laibow-Koser [email protected]
wrote:

Msan M. wrote in post #977110:

I have Ruser and Bag models.
I’m using Ruser instead of User because I know “user” create problems
with a postgresql database.

You may “know” it, but it ain’t true. I use User models in PostgreSQL
all the time.

That’s because rails translate User in users table.

Msan M. wrote in post #977110:

I have Ruser and Bag models.
I’m using Ruser instead of User because I know “user” create problems
with a postgresql database.

You may “know” it, but it ain’t true. I use User models in PostgreSQL
all the time.

Bags must be delivered to Ruser so I need to know when one or more
bags are delivered.
I create these associations:

class Ruser < ActiveRecord::Base
has_many :deliveries
has_many :bags, :through => :deliveries

class Bag < ActiveRecord::Base
has_many :deliveries
has_many :rusers, :through => :deliveries

class Delivery < ActiveRecord::Base
belongs_to :ruser
belongs_to :bag

Ruser and Bag are already populated.
Delivery has delivered_at attribute.

Now I want delivery 3 bags to ruser.
I can do
bags=Bags.all
user=Ruser.find(params[:id])
delivery = user.deliveries.create(:delivered_at => Date.today).
But I can’t do delivery.bags << bags.
I think that could be some mistakes in my associations.

Of course you can’t do delivery.bags . You’ve specified Delivery
belongs_to :bag, which means that there’s only one bag for a delivery to
be associated with. Perhaps you wanted Delivery has_many :bags.

You probably also wanted Bag belongs_to :delivery : how can one bag be
part of several deliveries?

I think you need to analyze the relationships between your models a
little better.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

On 24 January 2011 16:04, Mauro [email protected] wrote:

On 24 January 2011 15:33, Marnen Laibow-Koser [email protected] wrote:

Msan M. wrote in post #977110:

I have Ruser and Bag models.
I’m using Ruser instead of User because I know “user” create problems
with a postgresql database.

You may “know” it, but it ain’t true. I use User models in PostgreSQL
all the time.

That’s because rails translate User in users table.

What do you mean by that, I do not understand.

Colin

On 24 January 2011 17:21, Colin L. [email protected] wrote:

That’s because rails translate User in users table.

What do you mean by that, I do not understand.

nothing important, I work with other frameworks, like grails, that do
not pluralized
the model name when creating the table so I used not to use “user”.

Msan M. wrote in post #977159:

On 24 January 2011 15:33, Marnen Laibow-Koser [email protected]
wrote:

Msan M. wrote in post #977110:

I have Ruser and Bag models.
I’m using Ruser instead of User because I know “user” create problems
with a postgresql database.

You may “know” it, but it ain’t true. I use User models in PostgreSQL
all the time.

That’s because rails translate User in users table.

And your point is…?

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

On 24 January 2011 19:03, Mauro [email protected] wrote:

That’s because rails translate User in users table.

What do you mean by that, I do not understand.

nothing important, I work with other frameworks, like grails, that do
not pluralized
the model name when creating the table so I used not to use “user”.

You have still not explained why you are not using a class name User
and table name users. You appeared to suggest that this is not
possible when using PostgreSQL.

Colin

Colin

On 24 January 2011 21:42, Colin L. [email protected] wrote:

all the time.
and table name users. You appeared to suggest that this is not
possible when using PostgreSQL.

I was confused thinking about grails.
With rails “User” can be used, with grails don’t.

On Jan 24, 2011, at 4:17 PM, Mauro wrote:

I was confused thinking about grails.
With rails “User” can be used, with grails don’t.

Why not? I have Grails apps with a User domain class that work just
fine. Although as a best practice in the Grails world you want to
package your domain classes, so it’ll probably have a table like
com.myco.user.

But I’ve done quick demo’s in Grails with an unpackaged User class with
no problem at all.

Best Wishes,
Peter

On 24 January 2011 22:44, Peter B. [email protected] wrote:

On Jan 24, 2011, at 4:17 PM, Mauro wrote:

I was confused thinking about grails.
With rails “User” can be used, with grails don’t.

Why not? I have Grails apps with a User domain class that work just fine.
Although as a best practice in the Grails world you want to package your domain
classes, so it’ll probably have a table like com.myco.user.

But I’ve done quick demo’s in Grails with an unpackaged User class with no
problem at all.

It’s an OT but the name tables that grails creates are not like you
said.
If you create a domain class com.myco.user the table that grails
create is simply user and postgres doesn’t like to have a table called
user.