On 12/6/06, Jason Vogel
<jasonvogel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:>
> I''m new to RoR. I''m [trying to at least] to set up an
associative
> table relationship:
>
> ProductVersion has many ProductCoverages''s and
> Coverages has many ProductCoverages''s.
>
>
-------------------------------------------------------------------------------------------
> class ProductVersion < ActiveRecord::Base
>
> require ''product_coverage''
>
> set_table_name "product_version"
> set_primary_key "product_version_id"
>
> has_many :product_coverages
> has_many :coverages, :through => :product_coverages
> ...
>
-------------------------------------------------------------------------------------------
> class Coverage < ActiveRecord::Base
>
> set_table_name "coverages"
> set_primary_key "coverage_id"
>
> has_many :product_coverages
> has_many :product_versions, :through => :product_coverages
> ...
>
-------------------------------------------------------------------------------------------
> class ProductCoverage < ActiveRecord::Base
>
> belongs_to :product_version
> belongs_to :coverage
>
> attr_accessor :external_description
>
> def self.find_coverages(product_version_id,coverage_type)
>
> find_by_sql ["select product_coverage.* from coverages,
> product_coverage " +
> "where coverages.coverage_id >
product_coverage.coverage_id and product_coverage.product_version_id > ?
" +
> " and optional_base_type = ?",
> product_version_id,coverage_type]
> end
> end
>
-------------------------------------------------------------------------------------------
>
> First, is my HABTM setup correctly?
Looks ok.
If I understand your code correctly, you are trying to get all
Coverages for a given ProductVersion (possibly with an optional
base_type). In this case, you can write the query much more nicely
though:
ProductVersion.find(product_version_id).coverages.find_by_coverage_type(optional_base_type)
or if no optional_base_type is given:
ProductVersion.find(product_version_id).coverages
Only drawback is that this will use two queries instead of one, but
that should not be a problem in almost all cases.
If you really want the ProductCoverage instances (even though that
does not seem to make sense to me), use something like:
ProductVersion.find(product_version_id).product_coverages.find(:all,
:conditions=>[''coverages.coverage_type=?'',
optional_base_type],
:include=>:coverages)
>
> Second, what I would like to try is that I would like to add that
> "external_description" column. It''s a column that is
available within
> the SQL query (but on COVERAGES). Is there a way to do this?
>
Not clear on what you mean. If there is a column called
external_description in the coverages table, this will be available on
any Coverage object. The following should work:
ProductVersion.find(product_version_id).coverages.each do |c|
p c.external_description
end
Maybe I don''t understand you correctly...
Cheers,
Max
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---