Then I can do user.deliveries.where(:delivered_at =>
d1 = user.deliveries.create(:bag => bag1, :delivered_at => Time.now)
user.deliveries # => [d1]
user.bags # => [bag1] - Performs the query, finds the bag
through the delivery, caches it
d2 = user.deliveries.create(:bag => bag2, :delivered_at => 1.day.ago)
user.deliveries # => [d1, d2] - Using the association to create made
it know about the new one
user.bags # => [bag1] - Still cached from above
user.bags.reload # => [bag1, bag2] - Forces it to rerun the query
So I have to create deliveries as many as bags to delivery.
If I select all bags together like:
bags=Bag.all
and I want to deliver these bags to user I have to create deliveries
under a bags.each block?
As Colin mentions though, you’re probably best using user.deliveries
<<, as that will both set your association IDs correctly, and have the
collection all happy.
About this point I’m still confused.
I want delivery one or more bags to ruser and I want to set the date
of delivery.
I’m thinking:
user=Ruser.first;
bags=Bag.all
user << bags,
this create deliveries but set delivered_at == nil.
Then I can do user.deliveries.where(:delivered_at =>
nil).update_all(:delivered_at => Date.today).
Can it be a solution?
So I have to create deliveries as many as bags to delivery.
If I select all bags together like:
bags=Bag.all
and I want to deliver these bags to user I have to create deliveries
under a bags.each block?
Correct, you need to create all the bags no matter what, so you’ll
want to loop through all the bags you want to create a delivery for
and use the previously referenced code. If you’re worried about the
performance of doing a bunch of queries there are some gems that can
help with that (http://www.continuousthinking.com/tags/arext is the
one I always see, but I haven’t used it) but I wouldn’t bother unless
you determine its a performance issue.
Also a note on your previous code:
user << bags
This won’t work as its bad syntax: first you’d want user.bags since
that’s the collection you’re adding to; and second << takes a single
object and adds it to an array, where as you want to either replace
the array (users.bags = bags) or concat another array (user.bags +=
bags). Also, from a performance perspective, I’m pretty sure either
of those would do bags.length insert calls too (just like a .each loop
would); in other words AR associations don’t do any special bulk
insert for you, it just hides the loops from you.
want to loop through all the bags you want to create a delivery for
This won’t work as its bad syntax:
Yes I’ve make a mistake, it is user.bags << bags.
So I have to create deliveries as many as bags to delivery.
If I select all bags together like:
bags=Bag.all
and I want to deliver these bags to user I have to create deliveries
under a bags.each block?
I don’t think you have previously said that you want one delivery to
have many bags, is that what you want?
What are the relationships you have at the moment?
What are the relationships you have at the moment?
At the moment I have :
class Ruser < ActiveRecord::Base
has_many :deliveries
has_many :bags, :through => :deliveries
end
class Bag < ActiveRecord::Base
has_many :deliveries
has_many :rusers, :through => :deliveries
end
class Delivery < ActiveRecord::Base
belongs_to :ruser
belongs_to :bag
end
and Delivery has delivered_at as an extra attribute.
I can delivery one or more bags to user and I want to know the date in
which the bag or the bags is/are delivered.
If I deliver, for example 3 bags in date 2011-01-01, I have to iterate
over bags.each and do a user.deliveries.create(…) for every bag or
there is better solution for that?
have many bags, is that what you want?
You have not answered this question and you snipped my other question,
are the relationships correct for your requirement?
Please answer the first one and think about the second.
Sorry, that does not make sense. The question is whether a delivery
relates to only one bag (so delivery belongs_to bag) or a delivery
relates to many bags, in which case I imagine you want delivery
has_many bags, bag belongs_to delivery.
If you have to solve this problem: you have 3 types of bags, wet type,
dry type and type plastic.
These bags must be delivered to an user and I need to know the date of
delivery.
It can be delivered bags of one kind or also bags of all three types.
How do you create the associations?
I think what I have done is correct.
I don’t think you have previously said that you want one delivery to
have many bags, is that what you want?
You have not answered this question and you snipped my other question,
are the relationships correct for your requirement?
I’ve posted to have an opinion about it, that is if associations are correct.
Sorry, that does not make sense. The question is whether a delivery
relates to only one bag (so delivery belongs_to bag) or a delivery
relates to many bags, in which case I imagine you want delivery
has_many bags, bag belongs_to delivery.
In date 2011-01-01 I can deliver bag type “plastic” to user1,user2 and
user3 or in the same date I can deliver bag type “plastic”, “wet” and
“dry” to user1, user2 and user3, or I can do the same types of
deliveries only to one user.
So is a delivery something like a truck load of bags, with some bags
going to one user and other bags going to other users, all from the
same delivery? If that is the case in which table are you proposing
to save which bag(s) go to which user?
Colin meant that any single delivery is designed to be addressed to multiple
to save which bag(s) go to which user?
We are confusing and it 's my fault because of my poor English.
Back to the top.
I have a lot of users and I have three bag types: “plastic”, “wet” and
“dry”.
At date 2011-01-01 a user comes to me and I must deliver to him some
bags of type “plastic”, I give bags to him I must annotate the date of
deliver and what type of bags I gave.
Not matter the number of bags.
At the same date another user comes to me and I must give to him some
bags of type “plastic” and “wet”, I give the bags I must annotate the
date of deliver and what type of bags I gave.
Then comes another user and I must give bags of type “dry”, then comes
another user and I must give bags of all three types.
At date 2011-01-02 it can be the same story, and so on.
At the end I must know for every user how much deliveries, the dates
of every single delivery and for every single delivery what type of
bags I gave to him.