2011-11-27 40 views
0

我有一個產品元素有很多image_blog。從視圖中檢索關聯

我有一個image_blog id和一個包含該image_blog的@product。我怎樣才能訪問它?

我想它應該看起來像@product.image_blogs[:id=>"24"]。什麼是正確的語法?

回答

1

既然你知道image_blog的獨特id,你不需要@product訪問它:

ImageBlog.find(24) 

要訪問,比方說,第一image_blogid@product

@product.image_blogs.first.id 
1
@product.image_blogs.where(id:24) # ruby 1.9 new hash syntax 
@product.image_blogs.where(:id => 24) # previous hash syntax