Hi you all, I have a "has_and_belongs_to_many()" declaration between models "Pages" and "Properties". However, this relation has another attribute, called "value". I want to search the pages that have a specific property, but with an specific value. To find the pages with the specific property, I have the following method in the controller: def show_appears property = Property.find_by_name(@params[:name]) @pages_with_property = property.pages end But, how can I get from "@pages_with_property" the pages with the exact value from "@params[:value]"? I can do it stating the SQL statement as it is, but I suppose Rails may have a smarter way to do it :( Thanks. -- 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 -~----------~----~----~----~------~----~------~--~---
Damaris Fuentes wrote:> Hi you all, > I have a "has_and_belongs_to_many()" declaration between models "Pages" > and "Properties". However, this relation has another attribute, called > "value". > > I want to search the pages that have a specific property, but with an > specific value. > To find the pages with the specific property, I have the following > method in the controller: > def show_appears > property = Property.find_by_name(@params[:name]) > @pages_with_property = property.pages > end > But, how can I get from "@pages_with_property" the pages with the exact > value from "@params[:value]"? I can do it stating the SQL statement as > it is, but I suppose Rails may have a smarter way to do it :( > > Thanks.try: def show_appears property = Property.find_by_name(@params[:name]) @pages_with_property = property.pages.find(:all, :conditions => "value = #{params[:value]}") end That isnt the most secure way, but it should give you the Pages with properties of params[:name] and value of params[:value] --jake -- 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 -~----------~----~----~----~------~----~------~--~---
See has_many :through On 12/18/06, Damaris Fuentes <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Hi you all, > I have a "has_and_belongs_to_many()" declaration between models "Pages" > and "Properties". However, this relation has another attribute, called > "value".-- _Deirdre http://deirdre.net/ --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Deirdre Saoirse Moen wrote:> See has_many :through > > On 12/18/06, Damaris Fuentes <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> >> Hi you all, >> I have a "has_and_belongs_to_many()" declaration between models "Pages" >> and "Properties". However, this relation has another attribute, called >> "value". > > -- > _Deirdre http://deirdre.net/Yes, before the first answer I changed the relation to a has_many :thorugh. However, I had the same problem. But Jake''s lines of code worked fine :D Thanks! -- 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 -~----------~----~----~----~------~----~------~--~---
ryan.raaum-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Dec-19 13:44 UTC
Re: has_and_belongs_to_many() with attributes
On Dec 18, 1:18 pm, Jake Varghese <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Damaris Fuentes wrote: > > Hi you all, > > I have a "has_and_belongs_to_many()" declaration between models "Pages" > > and "Properties". However, this relation has another attribute, called > > "value". > > > I want to search the pages that have a specific property, but with an > > specific value. > > To find the pages with the specific property, I have the following > > method in the controller: > > def show_appears > > property = Property.find_by_name(@params[:name]) > > @pages_with_property = property.pages > > end > > But, how can I get from "@pages_with_property" the pages with the exact > > value from "@params[:value]"? I can do it stating the SQL statement as > > it is, but I suppose Rails may have a smarter way to do it :( > > > Thanks.try: > def show_appears > property = Property.find_by_name(@params[:name]) > @pages_with_property = property.pages.find(:all, :conditions => "value > = #{params[:value]}") > end > > That isnt the most secure way, but it should give you the Pages with > properties of params[:name] and value of params[:value]Indeed, vulnerable to SQL injection. Use the interpolated form of the conditions options: property.pages.find(:all, :conditions => ["value = ?", params[:value] ] ) (Otherwise the right answer though :)> > --jake > > -- > Posted viahttp://www.ruby-forum.com/.-- Ryan Raaum http://raaum.org http://rails.raaum.org -- Rails docs http://locomotive.raaum.org -- Self contained one-click Rails for Mac OS X --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---