See below...

Thanks!
Trish

On Aug 3, 11:00 am, Angel Robert Marquez <angel.marq...@gmail.com>
wrote:
> > Thank you for your response.  However, I am refactoring an existing
> > application, and am trying to remove a bunch of pure SQL that was put
> > in the code.  I believe the best way to do this is with a table
> > association.
>
> Why do you believe this?

This seems to be the best way to retrieve the needed data in the
proper order.  My closest attempt gets me two sets of data (for
department_products and store_products), and then I do a "find" on
their ids, allowing me to do the :order statement as needed.

>
> >  One of the big features I want to use out of this is
> > retrieving the records in the proper order (I'd prefer not to sort
> > with Ruby after the fact since that would force me to rewrite a bunch
> > of code for the sorting of the report).
>
> What is the proper order?
This report needs to have (and currently has) multi-column sort
capabilities

>
>
>
> However, as this is an existing application (similar in setup to the> example 
> I gave), I cannot remove the model, as there is a table
> > already associated with it, and the ids are being used.
>
> Gotcha.
>
>
>
> > Another option that would work is to use a 'joins' statement in my
> > finder method so I can sort products based on Store attributes with
> > something like this:
> > FROM products
> > LEFT JOIN departments on departements.id = products.department_id
> > LEFT JOIN stores on stores.id = products.store_id OR stores.id =
> > departments.store_id
>
> why not SELECT view INNER JOIN associations WHERE = AND  = OR =.
forgive my SQL... that is just an attempt to explain what I need.  SQL
is definitely not my strong suit.
>
>
>
> > However, I am also using an :include option for several associations,
> > and it seems that joins and include are mutually exclusive.  I'd
> > really had to have to write out all of the associations.
>
> I'll respond after I unravel what that means in my head.

I suppose what this all comes down to, is a way to achieve the
necessary SQL through ActiveRecord so I can properly determine the
table.column_names to sort on.  As I mentioned earlier, I am able to
get the needed product items with my doing a find on the sum of ids...
so I guess this is less of an association issue, and more of a joins
issue ActiveRecord.find so I can get my proper sorting order..

>
>
>
> > Any thoughts?
>
> Those are my thoughts. I'm newish and find attempting to help in turn helps
> me to learn. I hope you don't mind.

Any advise helps!  I appreciate your taking the time to think about
this.
>
>
>
> > Thanks!
>
> You are welcome
>
> > Trish
>
> Angel
>
>
>
>
>
> > On Aug 3, 2:11 am, Angel Robert Marquez <angel.marq...@gmail.com>
> > wrote:
> > > class Store < ActiveRecord::Base
> > > has_many: store_type
> > > has_many: product_type, :through => department
>
> > > class Department < ActiveRecord::Base
> > > belongs_to: store
> > > belongs_to: product
>
> > > class Product < ActiveRecord::Base
> > >   has_many :departments
> > >   has_many :stores, :through => department
> > > end
>
> > > I think you need to remove the StoreType as a model...
>
> > > On Mon, Aug 2, 2010 at 11:05 AM, Trish <trish.b...@gmail.com> wrote:
> > > > I am working on a project that has some complex table associations,
> > > > and I am having a hard time declaring this association in a Model, and
> > > > fear it can't be done.
>
> > > > Here is an example of my issue...
>
> > > > class StoreType < ActiveRecord::Base; end
> > > > class Store < ActiveRecord::Base; end
> > > > class Department < ActiveRecord::Base; end
> > > > class Product < ActiveRecord::Base; end
>
> > > > A StoreType has many stores and a Store has many Department.  However,
> > > > both Store and Department have many products.  An product has the
> > > > columns store_id and department_id which are mutually exclusive.  This
> > > > is because a Product may belong directly to a Department, or it may be
> > > > a 'global' product that belongs directly to the Store.
>
> > > > What I'd like to do is have an association in the StoreType model that
> > > > would give me all products for that StoreType.
>
> > > > Currently, I have set up the following associtations on StoreType:
>
> > > > class StoreType < ActiveRecord::Base
> > > >  has_many :stores
> > > >  has_many :departments, :through=>:stores
>
> > > > has_many :store_products, :through=>:stores, :source=>:products, :uniq
> > > > => true
>
> > > > has_many :department_products, :through=>:departments,
> > :source=>:products,
> > > > :uniq
> > > > => true
> > > > end
>
> > > > This is using the Nested Has Many Through plugin to achieve the nested
> > > > association (department_products).
>
> > > > However, I'd like to have a generic 'products' association for
> > > > StoreType that pulls a combination of the two product associations.
> > > > I'd like to do this through an association instead of just through a
> > > > function because I want to gain the dynamic methods created by
> > > > has_many, specifically the "collection.find(...)" method so I can add
> > > > more conditions to the collection.
>
> > > > Is there a way to do this?
>
> > > > Thanks in advance!
> > > > Trish
>
> > > > --
> > > > 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@googlegroups.com
> > .
> > > > To unsubscribe from this group, send email to
> > > > rubyonrails-talk+unsubscr...@googlegroups.com<rubyonrails-talk%2Bunsubscrib
> > > >  e...@googlegroups.com><rubyonrails-talk%2Bunsubscrib
> > e...@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-t...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > rubyonrails-talk+unsubscr...@googlegroups.com<rubyonrails-talk%2Bunsubscrib 
> > e...@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-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to