LOUPOS KONSTANTINOS
2009-Jun-01 21:01 UTC
Access values with has_and_belongs_to_many assocciation
Hi! I have 2 models author and product. class Author < ActiveRecord::Base has_and_belongs_to_many :products end class Product < ActiveRecord::Base has_and_belongs_to_many :authors end I have also created the migration for the jointable authors_products with the keys author_id and product_id. Now, i suppose that when i have eg an author with author_id = 1 and a product_id=4 in the join table, i should be able to access the product title of that product and the author name of my author. How can i access these values??? Thanx in advance for your time!
On Jun 1, 5:01 pm, LOUPOS KONSTANTINOS <loup...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi! > I have 2 models author and product. > > class Author < ActiveRecord::Base > has_and_belongs_to_many :products > end > > class Product < ActiveRecord::Base > has_and_belongs_to_many :authors > end > > I have also created the migration for the jointable authors_products > with the keys author_id and product_id. > > Now, i suppose that when i have eg an author with author_id = 1 and a > product_id=4 in the join table, i should be able to access the product > title of that product and the author name of my author. > > How can i access these values??? >The association adds some helper attributes to your model. If you have an instance of an author you can use the products attribute to get all of the associated products in an array: my_author = Author.find(1) my_author.products
Kostas L.
2009-Jun-03 19:30 UTC
Re: Access values with has_and_belongs_to_many assocciation
Thanx Brian. So if i want to see the product title i should write my_author.products.title??? Or i should use something else? Cause i''ve tried it but i get nothing On Jun 2, 3:10 am, Brian <butler.bria...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jun 1, 5:01 pm, LOUPOS KONSTANTINOS <loup...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Hi! > > I have 2 models author and product. > > > class Author < ActiveRecord::Base > > has_and_belongs_to_many :products > > end > > > class Product < ActiveRecord::Base > > has_and_belongs_to_many :authors > > end > > > I have also created the migration for the jointable authors_products > > with the keys author_id and product_id. > > > Now, i suppose that when i have eg an author with author_id = 1 and a > > product_id=4 in the join table, i should be able to access the product > > title of that product and the author name of my author. > > > How can i access these values??? > > The association adds some helper attributes to your model. If you > have an instance of an author you can use the products attribute to > get all of the associated products in an array: > > my_author = Author.find(1) > my_author.products
Colin Law
2009-Jun-03 21:12 UTC
Re: Access values with has_and_belongs_to_many assocciation
2009/6/3 Kostas L. <louposk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > Thanx Brian. > So if i want to see the product title i should write > my_author.products.title??? Or i should use something else? > Cause i''ve tried it but i get nothing >my_author.products gives you an array of all the products for that author, each of which has a title, so for example my_author.products[0].title will give you the title of the first one Colin
To get all the products titles of my_author you can do like my_author.products.map(&:title) Sijo -- Posted via http://www.ruby-forum.com/.
Konstantinos Loupos
2009-Jun-06 13:18 UTC
Re: Access values with has_and_belongs_to_many assocciation
Thanks! Your help was really usefull! On Thu, Jun 4, 2009 at 7:08 AM, Sijo Kg <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>wrote:> > To get all the products titles of my_author you can do like > > my_author.products.map(&:title) > > Sijo > -- > Posted via http://www.ruby-forum.com/. > > > >-- Κωνσταντίνος Λούπος louposk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Konstantinos Loupos
2009-Jun-06 13:24 UTC
Re: Access values with has_and_belongs_to_many assocciation
Sijo, map works great! but is there any other way showing the title but not using map? I am curious because i''ve been searching for this but i found nothing. Thank u Kostas On Thu, Jun 4, 2009 at 7:08 AM, Sijo Kg <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>wrote:> > To get all the products titles of my_author you can do like > > my_author.products.map(&:title) > > Sijo > -- > Posted via http://www.ruby-forum.com/. > > > >-- Κωνσταντίνος Λούπος louposk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Colin Law
2009-Jun-06 13:50 UTC
Re: Access values with has_and_belongs_to_many assocciation
2009/6/6 Konstantinos Loupos <louposk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> Sijo, > > map works great! > but is there any other way showing the title but not using map? > I am curious because i''ve been searching for this but i found nothing.What exactly do you mean by ''showing the title''. The map function returns an array containing the titles of all the products. What is it that you are trying to achieve? Colin> > Thank u > Kostas > > On Thu, Jun 4, 2009 at 7:08 AM, Sijo Kg <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: >> >> To get all the products titles of my_author you can do like >> >> my_author.products.map(&:title) >> >> Sijo >> -- >> Posted via http://www.ruby-forum.com/. >> >> > > > > -- > Κωνσταντίνος Λούπος > louposk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > > > >
Konstantinos Loupos
2009-Jun-08 16:29 UTC
Re: Access values with has_and_belongs_to_many assocciation
Thank u all. Colin sorry for my previous question i found the sollution i wanted. There is something else i want to do. I calculate the size of this array. If the size is less than 2, i want to see the name of the author, and if there are more than 2 authors i want to see both names with a party-comma between them. here is the code so far: <% if (product.authors.map(&:name).size) < 2 %> <%= "Author: Here i should see the name of the author" %> <% else %> <%= "Authors: Here i should see all the authors name separated with a party-comma."%> <% end %> How can i do that? Thank u for your time again! Kostas> 2009/6/6 Konstantinos Loupos <louposk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > Sijo, > > > > map works great! > > but is there any other way showing the title but not using map? > > I am curious because i''ve been searching for this but i found nothing. > > What exactly do you mean by ''showing the title''. The map function > returns an array containing the titles of all the products. What is > it that you are trying to achieve? > > Colin > > > > > Thank u > > Kostas > > > > On Thu, Jun 4, 2009 at 7:08 AM, Sijo Kg < > rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > wrote: > >> > >> To get all the products titles of my_author you can do like > >> > >> my_author.products.map(&:title) > >> > >> Sijo > >> -- > >> Posted via http://www.ruby-forum.com/. > >> > >> > > > > > > > > -- > > Κωνσταντίνος Λούπος > > louposk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > > > > > > > > > > >-- Κωνσταντίνος Λούπος louposk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Colin Law
2009-Jun-08 20:47 UTC
Re: Access values with has_and_belongs_to_many assocciation
2009/6/8 Konstantinos Loupos <louposk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> Thank u all. > Colin sorry for my previous question i found the sollution i wanted. > There is something else i want to do. I calculate the size of this array. If > the size is less than 2, i want to see the name of the author, and if there > are more than 2 authors i want to see both names with a party-comma between > them. > here is the code so far: > > <% if (product.authors.map(&:name).size) < 2 %> > <%= "Author: Here i should see the name of the author" %> > <% else %> > <%= "Authors: Here i should see all the authors name separated with a > party-comma."%> > <% end %> >No need to test for the special case of only one element, look up the docs for Array#join and try something like product.authors.map(&:name).join('','') I am not sure what a party-comma is. The only thing this won''t do is the s on the end of Author in the case of multiple. So you may need to do the test if you really need this. If so then product.authors.size < 2 will do, no need to convert it to an array of names before checking the size. There is one thing to watch out for though in both cases, suppose product has no authors at all, then product.authors will be nil and you would get an exception trying to call map or size on nil. In this case you may want to display something else. One way to tidy this up so you don''t end up with messy code in your view would be to add a method to the product model that returns the string you wish to see displayed and handles the nil case internally then the view would just contain <%= product.authors_text %> where authors_text is product model method. I think possibly you could usefully work through some basic tutorials on Ruby to get you going. Colin