I don''t find useful examples on using has_many through associations. First of all I need to know if it is necessary to have a model. I have this type of association: models: User, Bag. User has_many :deliveries, has_many :bags, :through => :deliveries, Bag has_many :deliveries, has_many :ruser, :through => :deliveries. I need to create Delivery model? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 22 January 2011 15:13, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I don''t find useful examples on using has_many through associations. > First of all I need to know if it is necessary to have a model. > I have this type of association: > models: User, Bag. > User has_many :deliveries, has_many :bags, :through => :deliveries, > Bag has_many :deliveries, has_many :ruser, :through => :deliveries. > I need to create Delivery model?Yes, Delivery belongs_to user and belongs_to bag. But if there is no other data in Delivery then you could use User has_and_belongs_to_many :bags Bag has_and_belongs_to_many :users then you don''t need deliveries, but you still need to provide a table bags_users containing the ids. I tend to favour having a model though, and using :through as you have suggested, especially if the join table has some meaning in the real world (such as deliveries). Have you seen the Rails Guide on ActiveRecord Associations, I think it should have examples of both of these strategies. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 22 January 2011 16:22, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 22 January 2011 15:13, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> I don''t find useful examples on using has_many through associations. >> First of all I need to know if it is necessary to have a model. >> I have this type of association: >> models: User, Bag. >> User has_many :deliveries, has_many :bags, :through => :deliveries, >> Bag has_many :deliveries, has_many :ruser, :through => :deliveries. >> I need to create Delivery model? > > Yes, Delivery belongs_to user and belongs_to bag. But if there is no > other data in Delivery then you could use > User has_and_belongs_to_many :bags > Bag has_and_belongs_to_many :users > then you don''t need deliveries, but you still need to provide a table > bags_users containing the ids.In Delivery I have the attribute delivered_at of type date. This is why I used :through => deliveries, I think this is correct. Isn''t it? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 22 January 2011 15:38, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 22 January 2011 16:22, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >> On 22 January 2011 15:13, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>> I don''t find useful examples on using has_many through associations. >>> First of all I need to know if it is necessary to have a model. >>> I have this type of association: >>> models: User, Bag. >>> User has_many :deliveries, has_many :bags, :through => :deliveries, >>> Bag has_many :deliveries, has_many :ruser, :through => :deliveries. >>> I need to create Delivery model? >> >> Yes, Delivery belongs_to user and belongs_to bag. But if there is no >> other data in Delivery then you could use >> User has_and_belongs_to_many :bags >> Bag has_and_belongs_to_many :users >> then you don''t need deliveries, but you still need to provide a table >> bags_users containing the ids. > > In Delivery I have the attribute delivered_at of type date. > This is why I used :through => deliveries, I think this is correct. > Isn''t it?Yes, that is fine. In that case you obviously need a Delivery model and a deliveries table, otherwise you will have no way of setting the date. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 22 January 2011 16:42, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> Yes, that is fine. In that case you obviously need a Delivery model > and a deliveries table, otherwise you will have no way of setting the > date.Ok, this now it''s clear. But how can I assign bags with delivered_at to users? I''ve created users and bags, then I''ve tried: user=User.first, bag=Bag.first Delivery.create(:user_id=>user.id, :bag_id=>bag.id: delivered_at=>''2011-01-01'') but when I do user.deliveries I have no data and also for user.bags. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 22 January 2011 15:47, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 22 January 2011 16:42, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > >> Yes, that is fine. In that case you obviously need a Delivery model >> and a deliveries table, otherwise you will have no way of setting the >> date. > > Ok, this now it''s clear. > But how can I assign bags with delivered_at to users? > I''ve created users and bags, then I''ve tried: > user=User.first, bag=Bag.first > Delivery.create(:user_id=>user.id, :bag_id=>bag.id: delivered_at=>''2011-01-01'') > but when I do user.deliveries I have no data and also for user.bags.There are much better ways of doing that, but that should work. Can you post the class definitions for your three classes, and also db/schema.rb? Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 22 January 2011 17:09, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 22 January 2011 15:47, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> On 22 January 2011 16:42, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >> >>> Yes, that is fine. In that case you obviously need a Delivery model >>> and a deliveries table, otherwise you will have no way of setting the >>> date. >> >> Ok, this now it''s clear. >> But how can I assign bags with delivered_at to users? >> I''ve created users and bags, then I''ve tried: >> user=User.first, bag=Bag.first >> Delivery.create(:user_id=>user.id, :bag_id=>bag.id: delivered_at=>''2011-01-01'') >> but when I do user.deliveries I have no data and also for user.bags. > > There are much better ways of doing that, but that should work. Can > you post the class definitions for your three classes, and also > db/schema.rb?Yes: class Ruser < ActiveRecord::Base has_many :deliveries has_many :bags, :through => :deliveries has_one :document default_scope :order => ''fullname ASC'' def self.search(fullname) where(''fullname ILIKE ?'', "#{fullname}%") if fullname end 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 ActiveRecord::Schema.define(:version => 20110114084202) do create_table "bags", :force => true do |t| t.string "bag_type" t.datetime "created_at" t.datetime "updated_at" end create_table "delegates", :force => true do |t| t.string "fullname" t.datetime "created_at" t.datetime "updated_at" end create_table "deliveries", :force => true do |t| t.integer "ruser_id", :limit => 10 t.integer "bag_id", :limit => 10 t.date "delivery_at" t.datetime "created_at" t.datetime "updated_at" end create_table "documents", :force => true do |t| t.string "doctype" t.string "docnumber" t.integer "ruser_id", :limit => 10 t.integer "delegate_id", :limit => 10 t.datetime "created_at" t.datetime "updated_at" end create_table "rusers", :force => true do |t| t.integer "year", :limit => 10 t.integer "code", :limit => 10 t.string "fullname" t.date "birthdate" t.string "homeaddress" t.string "homepostalcode", :limit => 6 t.string "homelocality" t.string "fiscalcode", :limit => 16 t.string "contractnumber" t.integer "squaremeters", :limit => 10 t.integer "category", :limit => 10 t.string "taxedaddress" t.datetime "created_at" t.datetime "updated_at" end end -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 22 January 2011 17:09, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> There are much better ways of doing that, but that should work.I''ve not found any exhaustive examples of has_many through. Which are the better ways? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 22 January 2011 16:13, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 22 January 2011 17:09, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >> On 22 January 2011 15:47, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>> On 22 January 2011 16:42, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >>> >>>> Yes, that is fine. In that case you obviously need a Delivery model >>>> and a deliveries table, otherwise you will have no way of setting the >>>> date. >>> >>> Ok, this now it''s clear. >>> But how can I assign bags with delivered_at to users? >>> I''ve created users and bags, then I''ve tried: >>> user=User.first, bag=Bag.first >>> Delivery.create(:user_id=>user.id, :bag_id=>bag.id: delivered_at=>''2011-01-01'') >>> but when I do user.deliveries I have no data and also for user.bags.This won''t work with the class definitions you post below as the class is Ruser not User. What happens if you open a ruby console (rails console if using rails 3, script/console otherwise) and enter the lines above (but using the correct class names)? Colin>> >> There are much better ways of doing that, but that should work. Can >> you post the class definitions for your three classes, and also >> db/schema.rb? > > Yes: > > class Ruser < ActiveRecord::Base > has_many :deliveries > has_many :bags, :through => :deliveries > has_one :document > default_scope :order => ''fullname ASC'' > > def self.search(fullname) > where(''fullname ILIKE ?'', "#{fullname}%") if fullname > end > 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 > > ActiveRecord::Schema.define(:version => 20110114084202) do > > create_table "bags", :force => true do |t| > t.string "bag_type" > t.datetime "created_at" > t.datetime "updated_at" > end > > create_table "delegates", :force => true do |t| > t.string "fullname" > t.datetime "created_at" > t.datetime "updated_at" > end > > create_table "deliveries", :force => true do |t| > t.integer "ruser_id", :limit => 10 > t.integer "bag_id", :limit => 10 > t.date "delivery_at" > t.datetime "created_at" > t.datetime "updated_at" > end > > create_table "documents", :force => true do |t| > t.string "doctype" > t.string "docnumber" > t.integer "ruser_id", :limit => 10 > t.integer "delegate_id", :limit => 10 > t.datetime "created_at" > t.datetime "updated_at" > > > end > > create_table "rusers", :force => true do |t| > t.integer "year", :limit => 10 > t.integer "code", :limit => 10 > t.string "fullname" > t.date "birthdate" > t.string "homeaddress" > t.string "homepostalcode", :limit => 6 > t.string "homelocality" > t.string "fiscalcode", :limit => 16 > t.string "contractnumber" > t.integer "squaremeters", :limit => 10 > t.integer "category", :limit => 10 > t.string "taxedaddress" > t.datetime "created_at" > t.datetime "updated_at" > end > > end > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 22 January 2011 16:16, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 22 January 2011 17:09, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > >> There are much better ways of doing that, but that should work. > > I''ve not found any exhaustive examples of has_many through. > Which are the better ways?Did you work through the Rails Guide on Associations as I suggested? Note particularly << and the .build methods. I would get what you have basically working first in the console as in my previous post. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 22 January 2011 17:25, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 22 January 2011 16:13, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> On 22 January 2011 17:09, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >>> On 22 January 2011 15:47, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>>> On 22 January 2011 16:42, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >>>> >>>>> Yes, that is fine. In that case you obviously need a Delivery model >>>>> and a deliveries table, otherwise you will have no way of setting the >>>>> date. >>>> >>>> Ok, this now it''s clear. >>>> But how can I assign bags with delivered_at to users? >>>> I''ve created users and bags, then I''ve tried: >>>> user=User.first, bag=Bag.first >>>> Delivery.create(:user_id=>user.id, :bag_id=>bag.id: delivered_at=>''2011-01-01'') >>>> but when I do user.deliveries I have no data and also for user.bags. > > This won''t work with the class definitions you post below as the class > is Ruser not User.Sorry for my mistake but what I''ve posted is correct, I''ve write user=User.first but it is user=Ruser.first.> What happens if you open a ruby console (rails console if using rails > 3, script/console otherwise) and enter the lines above (but using the > correct class names)?user=Ruser.first bag=Bag.first Delivery.create(:ruser_id=>user.id, :bag_id=>bag.id,:delivery_at=>''2011-01-01'') user.deliveries [] user.bags [] -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 22 January 2011 16:33, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 22 January 2011 17:25, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >> On 22 January 2011 16:13, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>> On 22 January 2011 17:09, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >>>> On 22 January 2011 15:47, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>>>> On 22 January 2011 16:42, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >>>>> >>>>>> Yes, that is fine. In that case you obviously need a Delivery model >>>>>> and a deliveries table, otherwise you will have no way of setting the >>>>>> date. >>>>> >>>>> Ok, this now it''s clear. >>>>> But how can I assign bags with delivered_at to users? >>>>> I''ve created users and bags, then I''ve tried: >>>>> user=User.first, bag=Bag.first >>>>> Delivery.create(:user_id=>user.id, :bag_id=>bag.id: delivered_at=>''2011-01-01'') >>>>> but when I do user.deliveries I have no data and also for user.bags. >> >> This won''t work with the class definitions you post below as the class >> is Ruser not User. > > Sorry for my mistake but what I''ve posted is correct, I''ve write > user=User.first but it is user=Ruser.first. > >> What happens if you open a ruby console (rails console if using rails >> 3, script/console otherwise) and enter the lines above (but using the >> correct class names)? > > user=Ruser.first > bag=Bag.first > Delivery.create(:ruser_id=>user.id, :bag_id=>bag.id,:delivery_at=>''2011-01-01'') > user.deliveries > [] > user.bags > []Can you post the complete console output please. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 22 January 2011 17:39, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> Can you post the complete console output please.It works if I close and restart console. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 22 January 2011 16:42, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 22 January 2011 17:39, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > >> Can you post the complete console output please. > > It works if I close and restart console.Closed and restarted after doing what? Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 22 January 2011 17:51, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 22 January 2011 16:42, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> On 22 January 2011 17:39, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >> >>> Can you post the complete console output please. >> >> It works if I close and restart console. > > Closed and restarted after doing what?I do: irb(main):001:0> user=Ruser.first => #<Ruser id: 82469, year: 2010, code: 100037787, fullname: "zzzzzz.", birthdate: nil, homeaddress: "xxxxxxxxx", homepostalcode: "11111", homelocality: "zzzzzzzz", fiscalcode: "0111111111", contractnumber: "30236", squaremeters: 56, category: 101, taxedaddress: "zzzzzzzzzzz\r\n", created_at: "2011-01-15 17:26:44", updated_at: "2011-01-15 17:26:44"> irb(main):004:0> bag=Bag.first => #<Bag id: 1, bag_type: "secco", created_at: "2011-01-15 23:32:01", updated_at: "2011-01-15 23:32:01"> irb(main):010:0> Delivery.create(:ruser_id=>user.id, :bag_id=>bag.id,:delivery_at=>''2011-01-01'') => #<Delivery id: 23, ruser_id: 82469, bag_id: 1, delivery_at: "2011-01-01", created_at: "2011-01-22 16:32:10", updated_at: "2011-01-22 16:32:10"> irb(main):012:0> user.deliveries => [] irb(main):013:0> user.bags => [] irb(main):014:0> exit jruby -S rails console irb(main):002:0> user.deliveries => [#<Delivery id: 23, ruser_id: 82469, bag_id: 1, delivery_at: "2011-01-01", created_at: "2011-01-22 16:32:10", updated_at: "2011-01-22 16:32:10">] irb(main):003:0> user.bags => [#<Bag id: 1, bag_type: "secco", created_at: "2011-01-15 23:32:01", updated_at: "2011-01-15 23:32:01">] -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 22 January 2011 16:58, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 22 January 2011 17:51, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >> On 22 January 2011 16:42, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>> On 22 January 2011 17:39, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >>> >>>> Can you post the complete console output please. >>> >>> It works if I close and restart console. >> >> Closed and restarted after doing what? > > I do: > > > irb(main):001:0> user=Ruser.first > => #<Ruser id: 82469, year: 2010, code: 100037787, fullname: > "zzzzzz.", birthdate: nil, homeaddress: "xxxxxxxxx", homepostalcode: > "11111", homelocality: "zzzzzzzz", fiscalcode: "0111111111", > contractnumber: "30236", squaremeters: 56, category: 101, > taxedaddress: "zzzzzzzzzzz\r\n", created_at: "2011-01-15 17:26:44", > updated_at: "2011-01-15 17:26:44"> > > irb(main):004:0> bag=Bag.first > => #<Bag id: 1, bag_type: "secco", created_at: "2011-01-15 23:32:01", > updated_at: "2011-01-15 23:32:01"> > > irb(main):010:0> Delivery.create(:ruser_id=>user.id, > :bag_id=>bag.id,:delivery_at=>''2011-01-01'') > => #<Delivery id: 23, ruser_id: 82469, bag_id: 1, delivery_at: > "2011-01-01", created_at: "2011-01-22 16:32:10", updated_at: > "2011-01-22 16:32:10"> > > irb(main):012:0> user.deliveries > => [] > irb(main):013:0> user.bags > => [] > > irb(main):014:0> exit > > jruby -S rails console > > irb(main):002:0> user.deliveries > => [#<Delivery id: 23, ruser_id: 82469, bag_id: 1, delivery_at: > "2011-01-01", created_at: "2011-01-22 16:32:10", updated_at: > "2011-01-22 16:32:10">] > irb(main):003:0> user.bags > => [#<Bag id: 1, bag_type: "secco", created_at: "2011-01-15 23:32:01", > updated_at: "2011-01-15 23:32:01">]That is interesting, I guess it is because you have already loaded user and have not told it that its deliveries have changed under its feet so to speak. I suspect that if you did user = Ruser.first again it would be ok. If you use the << or build methods then you should not have that problem. You said previously that you had problems finding tutorials with the through construct, if you work through some just using has_many and belongs_to you will get the basic principles and the through bit should not be a problem. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 22 January 2011 18:15, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> That is interesting, I guess it is because you have already loaded > user and have not told it that its deliveries have changed under its > feet so to speak. I suspect that if you did user = Ruser.first again > it would be ok. If you use the << or build methods then you should > not have that problem. You said previously that you had problems > finding tutorials with the through construct, if you work through some > just using has_many and belongs_to you will get the basic principles > and the through bit should not be a problem.Thanks for your help. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
ppgengler-vA8bmGSXo1KakBO8gow8eQ@public.gmane.org
2011-Jan-24 17:06 UTC
Re: has_many :through best practices.
On Jan 22, 10:58 am, Mauro <mrsan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 22 January 2011 17:51, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > > irb(main):012:0> user.deliveries > => [] > irb(main):013:0> user.bags > => [] > > irb(main):014:0> exit > > jruby -S rails console > > irb(main):002:0> user.deliveries > => [#<Delivery id: 23, ruser_id: 82469, bag_id: 1, delivery_at: > "2011-01-01", created_at: "2011-01-22 16:32:10", updated_at: > "2011-01-22 16:32:10">] > irb(main):003:0> user.bags > => [#<Bag id: 1, bag_type: "secco", created_at: "2011-01-15 23:32:01", > updated_at: "2011-01-15 23:32:01">]In this scenario .deliveries and .bags are actually AssociationProxy objects, which have a .reload method. So above, before you exit your console you could have done user.deliveries.reload user.bags.reload And you would have gotten the correct collection. As a debugging tool, you can direct your rails logging to stdout (i.e. ActiveRecord::Base.logger = Logger.new(STDOUT)) and then you can see the order of SQL commands. In your example you would have seen the optimistic association loads when you did Ruser.first, and then when you did user.deliveries later you would have noticed no SQL query, indicating that it was using the cached value, and thus indicating you should do the .reload. 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. HTH, \Peter -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 24 January 2011 18:06, ppgengler-vA8bmGSXo1KakBO8gow8eQ@public.gmane.org <ppgengler-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jan 24, 1:16 pm, Mauro <mrsan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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?Sorry I missed the part about the delivered at. You want something like below; note that I added two items to show how the caching affects it: user = Ruser.first bag1 = Bag.find(...) bag2 = Bag.find(...) 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
you forgot to write the answer? :-) On 25 January 2011 17:32, Peter <ppgengler-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jan 24, 1:16 pm, Mauro <mrsan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >> 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? > > Sorry I missed the part about the delivered at. You want something > like below; note that I added two items to show how the caching > affects it: > > user = Ruser.first > bag1 = Bag.find(...) > bag2 = Bag.find(...) > 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 > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 25 January 2011 17:32, Peter <ppgengler-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jan 24, 1:16 pm, Mauro <mrsan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >> 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? > > Sorry I missed the part about the delivered at. You want something > like below; note that I added two items to show how the caching > affects it: > > user = Ruser.first > bag1 = Bag.find(...) > bag2 = Bag.find(...) > 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 querySo 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? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 25 January 2011 17:01, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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? Do they meet your requirements? Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jan 25, 11:01 am, Mauro <mrsan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 25 January 2011 17:32, Peter <ppgeng...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > 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 it’s a performance issue. Also a note on your previous code:> user << bagsThis 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. \Peter -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 25 January 2011 18:10, Peter <ppgengler-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jan 25, 11:01 am, Mauro <mrsan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> On 25 January 2011 17:32, Peter <ppgeng...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >> 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 it’s a performance issue. > > Also a note on your previous code: > >> user << bags > > This won''t work as its bad syntax:Yes I''ve make a mistake, it is user.bags << bags. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 25 January 2011 18:10, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 25 January 2011 17:01, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >> 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?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? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 25 January 2011 17:17, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 25 January 2011 18:10, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >> On 25 January 2011 17:01, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>> >>> 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?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. Colin>> >> 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? > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 25 January 2011 18:35, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 25 January 2011 17:17, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> On 25 January 2011 18:10, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >>> On 25 January 2011 17:01, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>>> >>>> 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? > > 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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 25 January 2011 17:55, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 25 January 2011 18:35, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >> On 25 January 2011 17:17, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>> On 25 January 2011 18:10, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >>>> On 25 January 2011 17:01, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>>>> >>>>> 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? >> >> 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. Colin> > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 25 January 2011 21:23, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> 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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 25 January 2011 22:15, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 25 January 2011 21:23, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >> 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.Does one delivery consist of a number of bags? Please answer yes or no. Can a particular bag be in more than one delivery? Please answer yes or no. Colin> > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 26 January 2011 08:53, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> Does one delivery consist of a number of bags? Please answer yes or no.Yes.> Can a particular bag be in more than one delivery? Please answer yes or no.Yes. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 26 January 2011 08:00, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 26 January 2011 08:53, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >> Does one delivery consist of a number of bags? Please answer yes or no. > > Yes.In which case Delivery belongs_to bag is not correct as this implies only one bag for each delivery.> >> Can a particular bag be in more than one delivery? Please answer yes or no. > > Yes.So a single bag can be in several deliveries? If so then you need has_and_belongs_to_many between deliveries and bags. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 26 January 2011 09:16, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 26 January 2011 08:00, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> On 26 January 2011 08:53, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >>> Does one delivery consist of a number of bags? Please answer yes or no. >> >> Yes. > > In which case Delivery belongs_to bag is not correct as this implies > only one bag for each delivery. > >> >>> Can a particular bag be in more than one delivery? Please answer yes or no. >> >> Yes. > > So a single bag can be in several deliveries? If so then you need > has_and_belongs_to_many between deliveries and bags. >Then the associations are: class Ruser < ActiveRecord::Base has_and_belongs_to_many :deliveries class Bag < ActiveRecord::Base has_and_belongs_to_many :deliveries class Delivery < ActiveRecord::Base has_and_belongs_to_many :rusers has_and_belongs_to_many :bags mmm I don''t like too much. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 26 January 2011 10:10, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 26 January 2011 09:16, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >> On 26 January 2011 08:00, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>> On 26 January 2011 08:53, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >>>> Does one delivery consist of a number of bags? Please answer yes or no. >>> >>> Yes. >> >> In which case Delivery belongs_to bag is not correct as this implies >> only one bag for each delivery. >> >>> >>>> Can a particular bag be in more than one delivery? Please answer yes or no. >>> >>> Yes. >> >> So a single bag can be in several deliveries? If so then you need >> has_and_belongs_to_many between deliveries and bags. >> > Then the associations are: > > class Ruser < ActiveRecord::Base > has_and_belongs_to_many :deliveries > > class Bag < ActiveRecord::Base > has_and_belongs_to_many :deliveries > > class Delivery < ActiveRecord::Base > has_and_belongs_to_many :rusers > has_and_belongs_to_many :bagsThat means that one delivery is to multiple users, is that what you want? Colin> > mmm I don''t like too much. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 26 January 2011 11:58, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 26 January 2011 10:10, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> On 26 January 2011 09:16, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >>> On 26 January 2011 08:00, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>>> On 26 January 2011 08:53, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >>>>> Does one delivery consist of a number of bags? Please answer yes or no. >>>> >>>> Yes. >>> >>> In which case Delivery belongs_to bag is not correct as this implies >>> only one bag for each delivery. >>> >>>> >>>>> Can a particular bag be in more than one delivery? Please answer yes or no. >>>> >>>> Yes. >>> >>> So a single bag can be in several deliveries? If so then you need >>> has_and_belongs_to_many between deliveries and bags. >>> >> Then the associations are: >> >> class Ruser < ActiveRecord::Base >> has_and_belongs_to_many :deliveries >> >> class Bag < ActiveRecord::Base >> has_and_belongs_to_many :deliveries >> >> class Delivery < ActiveRecord::Base >> has_and_belongs_to_many :rusers >> has_and_belongs_to_many :bags > > That means that one delivery is to multiple users, is that what you want?Yes in a single day I can do a lot of deliveries to different users. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
2011/1/26 Matthias Wächter <matthias-kN4ci1sbul1kr1T4yOqhyg@public.gmane.org>:> On 26.01.2011 13:20, Mauro wrote: >> >> On 26 January 2011 11:58, Colin Law<clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >>> >>> That means that one delivery is to multiple users, is that what you want? >> >> Yes in a single day I can do a lot of deliveries to different users. > > Colin meant that any single delivery is designed to be addressed to multiple > users, and I doubt that this is what you want.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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 26 January 2011 13:12, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 2011/1/26 Matthias Wächter <matthias-kN4ci1sbul1kr1T4yOqhyg@public.gmane.org>: >> On 26.01.2011 13:20, Mauro wrote: >>> >>> On 26 January 2011 11:58, Colin Law<clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >>>> >>>> That means that one delivery is to multiple users, is that what you want? >>> >>> Yes in a single day I can do a lot of deliveries to different users. >> >> Colin meant that any single delivery is designed to be addressed to multiple >> users, and I doubt that this is what you want. > > 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 26 January 2011 14:42, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 26 January 2011 13:12, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> 2011/1/26 Matthias Wächter <matthias-kN4ci1sbul1kr1T4yOqhyg@public.gmane.org>: >>> On 26.01.2011 13:20, Mauro wrote: >>>> >>>> On 26 January 2011 11:58, Colin Law<clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >>>>> >>>>> That means that one delivery is to multiple users, is that what you want? >>>> >>>> Yes in a single day I can do a lot of deliveries to different users. >>> >>> Colin meant that any single delivery is designed to be addressed to multiple >>> users, and I doubt that this is what you want. >> >> 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?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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 26 January 2011 13:55, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 26 January 2011 14:42, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >> On 26 January 2011 13:12, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>> 2011/1/26 Matthias Wächter <matthias-kN4ci1sbul1kr1T4yOqhyg@public.gmane.org>: >>>> On 26.01.2011 13:20, Mauro wrote: >>>>> >>>>> On 26 January 2011 11:58, Colin Law<clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >>>>>> >>>>>> That means that one delivery is to multiple users, is that what you want? >>>>> >>>>> Yes in a single day I can do a lot of deliveries to different users. >>>> >>>> Colin meant that any single delivery is designed to be addressed to multiple >>>> users, and I doubt that this is what you want. >>> >>> 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? > > We are confusing and it ''s my fault because of my poor English.That is understandable when you are not using your own language. However it does mean that you must read every reply very carefully in order to try to understand what is being said.> 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.So do you count that as one delivery, one user, three bags?> 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.Another delivery, different user, and so on?> 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.One more point of confusion I think. When you talk about your bags table is that a record for each bag that will exist or is it just a table of bag *types*, in which case make the class BagType and table bag_types Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
2011/1/26 Matthias Wächter <matthias-kN4ci1sbul1kr1T4yOqhyg@public.gmane.org>:> On 26.01.2011 14:55, Mauro wrote: >> >> 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. > > Now we have a story! I think we should rephrase some terms to make stuff > easier. > > Let’s say, a single customer (class Customer) comes, and what he does is to > place an order (class Order). > > class Customer < ActiveRecord::Base > has_many :orders > > class Order < ActiveRecord::Base > belongs_to :customer > > > »customer« has the typical attributes like name and address. »Order«’s > attribute would be the order date. > > Now an order consists of multiple order items (class OrderItem), and each > order item refers to the product (class Product). > > class Customer < ActiveRecord::Base > has_many :orders > > class Order < ActiveRecord::Base > belongs_to :customer > has_many :order_items > has_many :products, :through => :order_items > > class OrderItem < ActiveRecord::Base > belongs_to :order > belongs_to :product > has_one :customer, :through => :order > > class Product < ActiveRecord::Base > has_many :order_items > has_many :orders, :through => :order_items > > > »Product« has typical attributes like name, price and weight per piece, and > so on. »OrderItem« has attributes like number of items ordered, actual price > granted for that products item (after discount), and so on. You will want to > extend »Order« as well to cover maybe a total price, total discount, or have > some delivery tracking. > > This can be easily mapped or renamed to what you need. You have to keep in > mind that you line up all your orders in a separate table, but this table > does not contain the actual ordered items as these are again in a separate > table, OrderItem, which by itself just refers to the actual product.I thought for some days and I think this is the best solution. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.