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 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.
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.
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.
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”.
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.
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.
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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.