I have a blog application where I would like to display on the post view a list of keyword categories and associated keyword types. #views/post.html.erb . . . <% @post.keywords.group_by(&:keyword_category_id).each do |category, type| %> <li><%= category %></li> <% type.each do |type| %> <%= type.keyword_type_id %> <% end %> <% end %> . . . The keyword categories and keyword types are in separate models. The association for the models are: #keyword_category.rb has_many :keyword_types #keyword_type.rb belongs_to :keyword_category #post.rb has_many :keywords has_many :keyword_types, through: :keywords has_many :keyword_categories, through: :keywords I am using a join table called ''keywords'', for keeping track of the keyword categories and keyword types for a post. The keywords table has the following fields. post_id keyword_category_id keyword_type_id The issue I am having is the view is displaying the ids and I can''t figure out how to do something like category.keyword_category.name? Any ideas on what I am doing wrong? Thanks in advance for your help. -- 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 To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/yACrHxSm9pQJ. For more options, visit https://groups.google.com/groups/opt_out.
On Aug 15, 2012, at 11:14 AM, AmateurCoder wrote:> I have a blog application where I would like to display on the post view a list of keyword categories and associated keyword types. > > #views/post.html.erb > > . > . > . > <% @post.keywords.group_by(&:keyword_category_id).each do |category, type| %> > <li><%= category %></li> > <% type.each do |type| %> > <%= type.keyword_type_id %> > <% end %> > <% end %> > . > . > . > > > The keyword categories and keyword types are in separate models. The association for the models are: > > #keyword_category.rb > has_many :keyword_types > > #keyword_type.rb > belongs_to :keyword_category > > #post.rb > > has_many :keywords > has_many :keyword_types, through: :keywords > has_many :keyword_categories, through: :keywords > > I am using a join table called ''keywords'', for keeping track of the keyword categories and keyword types for a post. > > The keywords table has the following fields. > post_id > keyword_category_id > keyword_type_id > > > > The issue I am having is the view is displaying the ids and I can''t figure out how to do something like category.keyword_category.name? Any ideas on what I am doing wrong? Thanks in advance for your help.That''s what your view is requesting. <%= type.keyword_type_id %> It''s not clear from the model code you have posted where the name lives, and whether it is the name of the category you want to show or the name of the keyword. Could you post your schema? Usually, if you have a whatever_id available, you can also get at the whatever itself directly, and then branch off of that to show the particular property of that object that you want to show. Maybe it''s this: <%= type.keyword_type.name %> But I''m not sure, because your relationships seem to be one-sided. Walter> > -- > 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. > To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/yACrHxSm9pQJ. > For more options, visit https://groups.google.com/groups/opt_out. > >-- 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 https://groups.google.com/groups/opt_out.
Walter, Thanks for your reply. I am going for a display like the following: Fruit Apple Orange Peach Meat Beef Fish Poultry Fruit and Meat are category names and apple, orange, peach, etc are type names. This information is being stored in a join table that look like the following: post_id keyword_category_id keyword_type_id 1 1 1 1 1 2 1 1 3 The schema look like the following: keywords table id :integer post_id: integer keyword_category_id :integer keyword_type_id :integer keyword_categories table id :integer name: string keyword_types table id: integer name: string keyword_category_id :integer I thought I could do the following: . <% @post.keywords.group_by(&:keyword_category).each do |category, type| %> <li><%= category.keyword_category.name %></li> <% type.each do |type| %> <%= type.keyword_type.name %> <% end %> <% end %>> This code doesn''t work.I had to use &:keyword_category_id as in the code below. The output look like 1 1 2 3 2 4 5 6 I want to display the category names and type names instead of the foreign key ids. I think I am using group_by wrong or even perhaps i need an association in the other direction. I am just baffled at the moment. Any ideas? On Wednesday, August 15, 2012 10:14:24 AM UTC-5, AmateurCoder wrote:> > I have a blog application where I would like to display on the post view a > list of keyword categories and associated keyword types. > > #views/post.html.erb > > . > . > . > <% @post.keywords.group_by(&:keyword_category_id).each do |category, type| > %> > <li><%= category %></li> > <% type.each do |type| %> > <%= type.keyword_type_id %> > <% end %> > <% end %> > . > . > . > > > The keyword categories and keyword types are in separate models. The > association for the models are: > > #keyword_category.rb > has_many :keyword_types > > #keyword_type.rb > belongs_to :keyword_category > > #post.rb > > has_many :keywords > has_many :keyword_types, through: :keywords > has_many :keyword_categories, through: :keywords > > I am using a join table called ''keywords'', for keeping track of the > keyword categories and keyword types for a post. > > The keywords table has the following fields. > post_id > keyword_category_id > keyword_type_id > > > > The issue I am having is the view is displaying the ids and I can''t figure > out how to do something like category.keyword_category.name? Any ideas > on what I am doing wrong? Thanks in advance for your help. >-- 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 To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/fSmxI0LAIrwJ. For more options, visit https://groups.google.com/groups/opt_out.
On Aug 16, 2012, at 3:45 PM, AmateurCoder wrote:> Walter, > > Thanks for your reply. > > I am going for a display like the following: > > > Fruit > Apple > Orange > Peach > Meat > Beef > Fish > Poultry > > Fruit and Meat are category names and apple, orange, peach, etc are type names. > > This information is being stored in a join table that look like the following: > > post_id keyword_category_id keyword_type_id > 1 1 1 > 1 1 2 > 1 1 3 > > The schema look like the following: > > keywords table > id :integer > post_id: integer > keyword_category_id :integer > keyword_type_id :integer > > keyword_categories table > id :integer > name: string > > keyword_types table > id: integer > name: string > keyword_category_id :integer > > I thought I could do the following: > . > <% @post.keywords.group_by(&:keyword_category).each do |category, type| %> > <li><%= category.keyword_category.name %></li> > <% type.each do |type| %> > <%= type.keyword_type.name %> > <% end %> > <% end %> > This code doesn''t work. > > I had to use &:keyword_category_id as in the code below. The output look like > > 1 > 1 > 2 > 3 > 2 > 4 > 5 > 6 > > I want to display the category names and type names instead of the foreign key ids. I think I am using group_by wrong or even perhaps i need an association in the other direction. I am just baffled at the moment. > > > Any ideas? > >You might want to look at this blog post (kind of old, but it might help): http://ariejan.net/2007/01/12/rails-group-results-by-week-using-group_by/ You might also want to read through the Rails Guide on Associations, and see if you can parse out what''s missing in your relationships. Make sure that each has_many is balanced on the other side by a belongs_to. Walter> > > On Wednesday, August 15, 2012 10:14:24 AM UTC-5, AmateurCoder wrote: > I have a blog application where I would like to display on the post view a list of keyword categories and associated keyword types. > > #views/post.html.erb > > . > . > . > <% @post.keywords.group_by(&:keyword_category_id).each do |category, type| %> > <li><%= category %></li> > <% type.each do |type| %> > <%= type.keyword_type_id %> > <% end %> > <% end %> > . > . > . > > > The keyword categories and keyword types are in separate models. The association for the models are: > > #keyword_category.rb > has_many :keyword_types > > #keyword_type.rb > belongs_to :keyword_category > > #post.rb > > has_many :keywords > has_many :keyword_types, through: :keywords > has_many :keyword_categories, through: :keywords > > I am using a join table called ''keywords'', for keeping track of the keyword categories and keyword types for a post. > > The keywords table has the following fields. > post_id > keyword_category_id > keyword_type_id > > > > The issue I am having is the view is displaying the ids and I can''t figure out how to do something like category.keyword_category.name? Any ideas on what I am doing wrong? Thanks in advance for your help. > > -- > 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 > To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/fSmxI0LAIrwJ. > For more options, visit https://groups.google.com/groups/opt_out. > >-- 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 https://groups.google.com/groups/opt_out.