I have 3 models here setup like thus: AttachmentType has_many :attachments Attachment belongs_to :page belongs_to :attachment_type Page has_many :attachments Attachment belongs to both an AttachmentType & Page. How can I find all AttachmentType by a page id? I suppose I could say that AttachmentType has_one :page, but that seems a bit unnecessary. -- 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.
On 9 April 2010 19:25, Brent <wejrowski-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have 3 models here setup like thus: > > AttachmentType > has_many :attachments > > Attachment > belongs_to :page > belongs_to :attachment_type > > Page > has_many :attachments > > How can I find all AttachmentType by a page id? >Page has_many :attachments has_many :attachment_types :through => :attachments -- 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.
Hmm.. OK I did this and I can grab attachment_types through a page. BUT I can''t grab the specific attachments in the attachment_types in a page.. for instance if I try this: @types = @page.attachment_types <% @types.each do |t| %> <%= render(:partial => ''partial/attachment'', :collection => t.attachments) %> <% end %> It looks like when I grab attachments from the type, it grabs ALL attachments from that type, not attachments specifically associated to that page. How can I tell it to do a :through, with a model that isn''t associated in the AttachmentType model? -- 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.
OK I think I found the solution: @types = @page.attachment_types <% @types.each do |t| %> <%= render(:partial => ''partial/attachment'', :collection => t.attachments.find_all_by_page_id(@page.id)) %> <% end %> didn''t know I could still call the find_all_by_page_id after I called it from a model. -- 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.