gamov
2010-Jun-15 11:44 UTC
Getting note only the class instances but also instances of subclasses on a has_many associations
Hello, I have a model like this: ## Model class ContainerCharge < ShipmentCharge belongs_to :container end class Container < ActiveRecord::Base has_many :container_charges end class HandlingContainerCharge < ContainerCharge end class ShippingContainerCharge < ContainerCharge end ## Controller #Please assume here that this container has many Handling and Shipping charges container.container_charges # <- doesn''t return the handling and shipping charges, only instances of the the super class ContainerCharge I would like the association to return all the container charges which should be the right thing to do.... Anyone knows how to do this? Regards, Gam. -- 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.
Frederick Cheung
2010-Jun-15 11:57 UTC
Re: Getting note only the class instances but also instances of subclasses on a has_many associations
On Jun 15, 12:44 pm, gamov <gamaud...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I would like the association to return all the container charges which > should be the right thing to do.... > Anyone knows how to do this?Does this only happen if cache_classes is set to false ? Fred> > Regards, > Gam.-- 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.
Rick DeNatale
2010-Jun-15 12:11 UTC
Re: Re: Getting note only the class instances but also instances of subclasses on a has_many associations
On Tue, Jun 15, 2010 at 7:57 AM, Frederick Cheung <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On Jun 15, 12:44 pm, gamov <gamaud...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> I would like the association to return all the container charges which >> should be the right thing to do.... >> Anyone knows how to do this? > > Does this only happen if cache_classes is set to false ? > > FredFred, I don''t think that''s the issue here. It seems to me that the problem is the two-level STI ShippingCharge ContainerCharge HandlingContainerCharge ShippingContainerCharge So, since ContainerCharge is an STI subclass, the association has_many :container_charges is going to add a where clause restricting the results to those where the type is "ContainerCharge". I haven''t tested it but perhaps has_many :container_charges, :class_name => ''ShippingCharge'', :conditions => {:type => %w(ContainerCharge HandlingContainerCharge ShippingContainerCharge)} -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Github: http://github.com/rubyredrick Twitter: @RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale -- 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.
gamov
2010-Jun-15 12:13 UTC
Re: Getting note only the class instances but also instances of subclasses on a has_many associations
Hi Fred, nope, with cache_classes = true it behaves the same way... On Jun 15, 7:57 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jun 15, 12:44 pm, gamov <gamaud...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I would like the association to return all the container charges which > > should be the right thing to do.... > > Anyone knows how to do this? > > Does this only happen if cache_classes is set to false ? > > Fred > > > > > > > Regards, > > Gam.-- 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.
Frederick Cheung
2010-Jun-15 13:06 UTC
Re: Getting note only the class instances but also instances of subclasses on a has_many associations
On Jun 15, 1:11 pm, Rick DeNatale <rick.denat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I don''t think that''s the issue here. It seems to me that the problem > is the two-level STI > > ShippingCharge > ContainerCharge > HandlingContainerCharge > ShippingContainerCharge > > So, since ContainerCharge is an STI subclass, the association > > has_many :container_charges > > is going to add a where clause restricting the results to those where > the type is "ContainerCharge". >STI is a bit funny in development mode - if the class isn''t loaded, rails doesn''t know that it exists and so doesn''t know to include that particular type in the where condition on type. I''ve got STI mixed with associations in a few places and (as long as it knows that the subclasses exist) a has_many like that does pull in subclasses. I normally end up using require_dependency on the subclasses at the bottom of the file defining the parent class Fred> I haven''t tested it but perhaps > > has_many :container_charges, > :class_name => ''ShippingCharge'', > :conditions => {:type => %w(ContainerCharge > HandlingContainerCharge ShippingContainerCharge)} > > -- > Rick DeNatale > > Blog:http://talklikeaduck.denhaven2.com/ > Github:http://github.com/rubyredrick > Twitter: @RickDeNatale > WWR:http://www.workingwithrails.com/person/9021-rick-denatale > LinkedIn:http://www.linkedin.com/in/rickdenatale-- 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.
gamov
2010-Jun-15 16:43 UTC
Re: Getting note only the class instances but also instances of subclasses on a has_many associations
Hi Rick, Of course I should have looked at the SQL query... and indeed, you are perfectly right, Rails adds a WHERE clause on type... and it has to to filter out the classes that are NOT involved: SELECT count(*) AS count_all FROM "charges" WHERE ("charges".container_id = 1) AND ( ("charges"."type" ''ContainerCharge'' ) ) Your idea is almost there, but Rails insists with the last AND clause on the type.... any idea on how to get ride of it?: SELECT count(*) AS count_all FROM "charges" WHERE ("charges".container_id = 1 AND ("charges"."type" IN (''ContainerCharge'',''HandlingContainerCharge'',''ShippingContainerCharge''))) AND ( ("charges"."type" = ''ContainerCharge'' ) ) Thanks guys for your time. REALLY appreciated! Regards, Gam. PS: I just realized that I didn''t specify that I''m using a STI... but you figured it out... On Jun 15, 8:11 pm, Rick DeNatale <rick.denat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Tue, Jun 15, 2010 at 7:57 AM, Frederick Cheung > > <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > On Jun 15, 12:44 pm, gamov <gamaud...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >> I would like the association to return all the container charges which > >> should be the right thing to do.... > >> Anyone knows how to do this? > > > Does this only happen if cache_classes is set to false ? > > > Fred > > Fred, > > I don''t think that''s the issue here. It seems to me that the problem > is the two-level STI > > ShippingCharge > ContainerCharge > HandlingContainerCharge > ShippingContainerCharge > > So, since ContainerCharge is an STI subclass, the association > > has_many :container_charges > > is going to add a where clause restricting the results to those where > the type is "ContainerCharge". > > I haven''t tested it but perhaps > > has_many :container_charges, > :class_name => ''ShippingCharge'', > :conditions => {:type => %w(ContainerCharge > HandlingContainerCharge ShippingContainerCharge)} > > -- > Rick DeNatale > > Blog:http://talklikeaduck.denhaven2.com/ > Github:http://github.com/rubyredrick > Twitter: @RickDeNatale > WWR:http://www.workingwithrails.com/person/9021-rick-denatale > LinkedIn:http://www.linkedin.com/in/rickdenatale-- 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.
Frederick Cheung
2010-Jun-15 16:47 UTC
Re: Getting note only the class instances but also instances of subclasses on a has_many associations
On Jun 15, 5:43 pm, gamov <gamaud...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi Rick, > Of course I should have looked at the SQL query... and indeed, you are > perfectly right, Rails adds a WHERE clause on type... and it has to to > filter out the classes that are NOT involved: > SELECT count(*) AS count_all FROM "charges" WHERE > ("charges".container_id = 1) AND ( ("charges"."type" > ''ContainerCharge'' ) ) >It should just be a case of ensuring all the subclasses of container_charge are loaded Fred> Your idea is almost there, but Rails insists with the last AND clause > on the type.... any idea on how to get ride of it?: > SELECT count(*) AS count_all FROM "charges" WHERE > ("charges".container_id = 1 AND ("charges"."type" IN > (''ContainerCharge'',''HandlingContainerCharge'',''ShippingContainerCharge''))) > AND ( ("charges"."type" = ''ContainerCharge'' ) ) > > Thanks guys for your time. REALLY appreciated! > Regards, > Gam. > > PS: I just realized that I didn''t specify that I''m using a STI... but > you figured it out... > > On Jun 15, 8:11 pm, Rick DeNatale <rick.denat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > On Tue, Jun 15, 2010 at 7:57 AM, Frederick Cheung > > > <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > On Jun 15, 12:44 pm, gamov <gamaud...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > >> I would like the association to return all the container charges which > > >> should be the right thing to do.... > > >> Anyone knows how to do this? > > > > Does this only happen if cache_classes is set to false ? > > > > Fred > > > Fred, > > > I don''t think that''s the issue here. It seems to me that the problem > > is the two-level STI > > > ShippingCharge > > ContainerCharge > > HandlingContainerCharge > > ShippingContainerCharge > > > So, since ContainerCharge is an STI subclass, the association > > > has_many :container_charges > > > is going to add a where clause restricting the results to those where > > the type is "ContainerCharge". > > > I haven''t tested it but perhaps > > > has_many :container_charges, > > :class_name => ''ShippingCharge'', > > :conditions => {:type => %w(ContainerCharge > > HandlingContainerCharge ShippingContainerCharge)} > > > -- > > Rick DeNatale > > > Blog:http://talklikeaduck.denhaven2.com/ > > Github:http://github.com/rubyredrick > > Twitter: @RickDeNatale > > WWR:http://www.workingwithrails.com/person/9021-rick-denatale > > LinkedIn:http://www.linkedin.com/in/rickdenatale-- 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.
gamov
2010-Jun-15 16:57 UTC
Re: Getting note only the class instances but also instances of subclasses on a has_many associations
I have added: has_many :container_charges, :class_name => ''Charge'', :conditions => {:type => %w(ContainerCharge HandlingContainerCharge ShippingContainerCharge)} With this, Rails remove the problematic ##AND ( ("charges"."type" = > ''ContainerCharge'' ) ) ##. This seems like a big workaround not so pretty but it works in the console. Now the field_for method doesn''t see the charges in a form_for and I don''t know why... I''m almost there! On Jun 16, 12:47 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jun 15, 5:43 pm, gamov <gamaud...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi Rick, > > Of course I should have looked at the SQL query... and indeed, you are > > perfectly right, Rails adds a WHERE clause on type... and it has to to > > filter out the classes that are NOT involved: > > SELECT count(*) AS count_all FROM "charges" WHERE > > ("charges".container_id = 1) AND ( ("charges"."type" > > ''ContainerCharge'' ) ) > > It should just be a case of ensuring all the subclasses of > container_charge are loaded > > Fred > > > > > Your idea is almost there, but Rails insists with the last AND clause > > on the type.... any idea on how to get ride of it?: > > SELECT count(*) AS count_all FROM "charges" WHERE > > ("charges".container_id = 1 AND ("charges"."type" IN > > (''ContainerCharge'',''HandlingContainerCharge'',''ShippingContainerCharge''))) > > AND ( ("charges"."type" = ''ContainerCharge'' ) ) > > > Thanks guys for your time. REALLY appreciated! > > Regards, > > Gam. > > > PS: I just realized that I didn''t specify that I''m using a STI... but > > you figured it out... > > > On Jun 15, 8:11 pm, Rick DeNatale <rick.denat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > On Tue, Jun 15, 2010 at 7:57 AM, Frederick Cheung > > > > <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > On Jun 15, 12:44 pm, gamov <gamaud...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > >> I would like the association to return all the container charges which > > > >> should be the right thing to do.... > > > >> Anyone knows how to do this? > > > > > Does this only happen if cache_classes is set to false ? > > > > > Fred > > > > Fred, > > > > I don''t think that''s the issue here. It seems to me that the problem > > > is the two-level STI > > > > ShippingCharge > > > ContainerCharge > > > HandlingContainerCharge > > > ShippingContainerCharge > > > > So, since ContainerCharge is an STI subclass, the association > > > > has_many :container_charges > > > > is going to add a where clause restricting the results to those where > > > the type is "ContainerCharge". > > > > I haven''t tested it but perhaps > > > > has_many :container_charges, > > > :class_name => ''ShippingCharge'', > > > :conditions => {:type => %w(ContainerCharge > > > HandlingContainerCharge ShippingContainerCharge)} > > > > -- > > > Rick DeNatale > > > > Blog:http://talklikeaduck.denhaven2.com/ > > > Github:http://github.com/rubyredrick > > > Twitter: @RickDeNatale > > > WWR:http://www.workingwithrails.com/person/9021-rick-denatale > > > LinkedIn:http://www.linkedin.com/in/rickdenatale-- 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.
gamov
2010-Jun-15 17:07 UTC
Re: Getting note only the class instances but also instances of subclasses on a has_many associations
> Now the field_for method doesn''t > see the charges in a form_for and I don''t know why... I''m almost > there!Silly me, forgot the: accepts_nested_attributes_for :container_charges Well, it''s hacky but it works. I''ll report here if I run into other troubles because of this trick. Regards, Gam. -- 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.
gamov
2010-Jun-16 08:58 UTC
Re: Getting note only the class instances but also instances of subclasses on a has_many associations
I ran into problem when I wanted to mass assign via the association. The class of the new object will be of the topclass. The solution is here: http://stackoverflow.com/questions/1601739/rails-attr-accessible-does-not-work-for-type and in your form, use :type_helper On Jun 16, 1:07 am, gamov <gamaud...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Now the field_for method doesn''t > > see the charges in a form_for and I don''t know why... I''m almost > > there! > > Silly me, forgot the: accepts_nested_attributes_for :container_charges > > Well, it''s hacky but it works. I''ll report here if I run into other > troubles because of this trick. > > Regards, > Gam.-- 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.