hy .. I have a problem with selecting data from my database. this is my database: users that has_and_belongs_to_many rights articles that has_and_belongs_to_many rights rights that has_and_belongs_to_many users and articles. If I have a user ID.. How can I get the articles which he has the rights to ? something like this? : @article_list = User.find_by_id(0).rights.articles but this doesn''t work I hope someone has the answer -- Posted via http://www.ruby-forum.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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
You have to do it yourself in Ruby if you do it like that, or you will need to write some SQL. class User < ActiveRecord::Base def articles @articles||= self.rights.collect{|right| right.articles.flatten}.flatten end end That will allow you to do u = User.find(1) @articles = u.articles Or you could write a more complex find_by_sql that would to that for you which may be more efficient. On 11/6/07, Heldop Slippers <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > hy .. > > > I have a problem with selecting data from my database. > this is my database: > > > users that has_and_belongs_to_many rights > articles that has_and_belongs_to_many rights > rights that has_and_belongs_to_many users and articles. > > > If I have a user ID.. How can I get the articles which he has the rights > to ? > > > something like this? : > > @article_list = User.find_by_id(0).rights.articles > > but this doesn''t work > I hope someone has the answer > -- > Posted via http://www.ruby-forum.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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I would add a .uniq! to the end as well since articles could belong to multiple rights and so could a user. def articles @articles||= self.rights.collect{|right| right.articles.flatten}.flatten.uniq! end -Bill Brian Hogan wrote:> You have to do it yourself in Ruby if you do it like that, or you will > need to write some SQL. > > class User < ActiveRecord::Base > > def articles > @articles||= self.rights.collect{|right| > right.articles.flatten}.flatten > end > > end > > > That will allow you to do > > u = User.find(1) > @articles = u.articles > > > > Or you could write a more complex find_by_sql that would to that for > you which may be more efficient. > > > > > > > > > On 11/6/07, *Heldop Slippers* <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org > <mailto:rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>> wrote: > > > hy .. > > > I have a problem with selecting data from my database. > this is my database: > > > users that has_and_belongs_to_many rights > articles that has_and_belongs_to_many rights > rights that has_and_belongs_to_many users and articles. > > > If I have a user ID.. How can I get the articles which he has the > rights > to ? > > > something like this? : > > @article_list = User.find_by_id(0).rights.articles > > but this doesn''t work > I hope someone has the answer > -- > Posted via http://www.ruby-forum.com/. > > > > > > >-- Sincerely, William Pratt --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
@William: Yeah. Good catch. I forgot to add that. On 11/6/07, William Pratt <billp-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote:> > I would add a .uniq! to the end as well since articles could belong to > multiple rights and so could a user. > > def articles > @articles||= self.rights.collect{|right| right.articles.flatten > }.flatten.uniq! > end > > -Bill > > Brian Hogan wrote: > > You have to do it yourself in Ruby if you do it like that, or you will > need to write some SQL. > > class User < ActiveRecord::Base > > def articles > @articles||= self.rights.collect{|right| right.articles.flatten > }.flatten > end > > end > > > That will allow you to do > > u = User.find(1) > @articles = u.articles > > > > Or you could write a more complex find_by_sql that would to that for you > which may be more efficient. > > > > > > > > > On 11/6/07, Heldop Slippers <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > > > > > > hy .. > > > > > > I have a problem with selecting data from my database. > > this is my database: > > > > > > users that has_and_belongs_to_many rights > > articles that has_and_belongs_to_many rights > > rights that has_and_belongs_to_many users and articles. > > > > > > If I have a user ID.. How can I get the articles which he has the rights > > > > to ? > > > > > > something like this? : > > > > @article_list = User.find_by_id(0).rights.articles > > > > but this doesn''t work > > I hope someone has the answer > > -- > > Posted via http://www.ruby-forum.com/. > > > > > > > > > > > > -- > Sincerely, > > William Pratt > > > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
THANXS !! (I was strugling with this problem for ages :P) -- Posted via http://www.ruby-forum.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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I''m pretty sure you can drop the first flatten and also, to avoid the n+1 query problem, adding :include would almost certainly be more efficient: def articles @articles||= self.rights.find(:all, :include => :articles).collect{|right| right.articles }.flatten.uniq! end -Bill William Pratt wrote:> I would add a .uniq! to the end as well since articles could belong to > multiple rights and so could a user. > > def articles > @articles||= self.rights.collect{|right| > right.articles.flatten}.flatten.uniq! > end > > -Bill > > Brian Hogan wrote: >> You have to do it yourself in Ruby if you do it like that, or you >> will need to write some SQL. >> >> class User < ActiveRecord::Base >> >> def articles >> @articles||= self.rights.collect{|right| >> right.articles.flatten}.flatten >> end >> >> end >> >> >> That will allow you to do >> >> u = User.find(1) >> @articles = u.articles >> >> >> >> Or you could write a more complex find_by_sql that would to that for >> you which may be more efficient. >> >> >> >> >> >> >> >> >> On 11/6/07, *Heldop Slippers* <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org >> <mailto:rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>> wrote: >> >> >> hy .. >> >> >> I have a problem with selecting data from my database. >> this is my database: >> >> >> users that has_and_belongs_to_many rights >> articles that has_and_belongs_to_many rights >> rights that has_and_belongs_to_many users and articles. >> >> >> If I have a user ID.. How can I get the articles which he has the >> rights >> to ? >> >> >> something like this? : >> >> @article_list = User.find_by_id(0).rights.articles >> >> but this doesn''t work >> I hope someone has the answer >> -- >> Posted via http://www.ruby-forum.com/. >> >> >> >> >> >> > > -- > Sincerely, > > William Pratt > > > >-- Sincerely, William Pratt --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
THANXs for all the replays but it doesn''t work.. if I try to do it but I get: undefined method `rights'' for #<ApplicationController:0x22a8eb8> I do this: in my application controller: @article_list ||= self.rights.find(:all, :include =>:articles) .collect{|right| right.articles }.flatten.uniq! and then it will do: (of course in a layout and not in the controller :P) <% for article in @article_list %> ... <% end %> does anyone now what the problem is here ? and yes my models work (I can without problems ask for al the articles with right is 1 etc.) -- Posted via http://www.ruby-forum.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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
In your User model, add this: def articles @article_list ||= self.rights.find(:all, :include =>:articles).collect{|right| right.articles }.flatten.uniq! end Then in your controller, call the articles method on the current user. Say you have @user in your controller, then this would work: @user.articles Note that the first method goes in the User model, not in your controller. -Bill Heldop Slippers wrote:> THANXs for all the replays > > but it doesn''t work.. > if I try to do it but I get: > undefined method `rights'' for #<ApplicationController:0x22a8eb8> > > I do this: > in my application controller: > @article_list ||= self.rights.find(:all, :include =>:articles) > .collect{|right| right.articles }.flatten.uniq! > > > and then it will do: (of course in a layout and not in the controller > :P) > <% for article in @article_list %> > ... > <% end %> > > > does anyone now what the problem is here ? > and yes my models work (I can without problems ask for al the articles > with right is 1 etc.) > > >-- Sincerely, William Pratt --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ok well I am an idiot ... beceause I can''t read :( ... (it was explained in the first post) Sorry and thanxs guys -- Posted via http://www.ruby-forum.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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---