Hi Tod

For finding the category 10 of shop 1, the following should work by using
Activerecord associations

Shop.find(1).categories.find(10)
or 
Shop.find(1).categories.find(:first, :conditions => 'category = 10')

rainer




Dharmdip Rathod wrote:
> 
> 
> Hi
> 
> I have a standard associations belongs_to, has_many  :through
> 
> 
> class Shop < ActiveRecord::Base
>   has_many :show_categories
>   has_many :categories, :through => :shop_categories
> end
> 
> class Category < ActiveRecord::Base
>   has_many :shop_categories
>   has_many :shops, :through => :shop_categories
> end
> 
> class ShopCategory < ActiveRecord::Base
>   belongs_to :shop
>   belongs_to :category
> end
> 
> There is one root category with hierarchical data for shops.
> shop_categories are holding some common fields for shop and category.
> mixed one record for one shop and category. So, for 100 categories, in
> shop_categories are 200 records, one for each shop.
> 
> For this example I have 2 shops.
> 
> Shop.first.category.find(10).shop_categories Rails are returning 2
> shop_categories records one for each shop. Something is wrong with
> associations because it should find in shop_categories all records for
> first shop.
> 
> What I need is to retrieve only one record from shop_categories for
> corresponding association.
> I have tried has_one :shop_category but it returns not one but first and
> always same never less if I try
> Shop.find(1).category.find(10).shop_categories or
> Shop.find(2).category.find(10).shop_categories
> 
> So, what is really happening with this association?
> I realy can not guess, and I have tried all sort of things with no
> results.
> 
> Logically this kind of chaining
> Shop.find(2).category.find(10).shop_categories should collect and join
> appropriate associations and return data only for shop 2.
> 
> Could any one give some clues? Or clarify this kind of relation. Maybe
> there is another way to retrieve this single data?
> 
> 
> Thanks,
> Tod
> -- 
> Posted via http://www.ruby-forum.com/.
> 
> > 
> 
> 

-- 
View this message in context: 
http://www.nabble.com/how-to-retrieve-single-record-through-chained-associations-tp22389318p22389815.html
Sent from the RubyOnRails Users mailing list archive at Nabble.com.


--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to