Dear all, I need to apply "find" instead of find_by_sql. Kindly suggest me. Because, params[gm] is not working with find_by_sql. Kindly justify whether my code below is in correct syntax... find_by_sql("SELECT column1 FROM table WHERE column2 = ''params[gm]'' ORDER BY column1") -- With Regards Palani Kannan. K Office: +49-531-2616-226 Mobile: +4917647098166 -- 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 Sep 17, 2010, at 11:01 AM, PalaniKannan K wrote:> Dear all, > > I need to apply "find" instead of find_by_sql. Kindly suggest me. > Because, params[gm] is not working with find_by_sql. Kindly justify > whether my code below is in correct syntax... > > find_by_sql("SELECT column1 FROM table WHERE column2 = ''params[gm]'' > ORDER BY column1") > > -- > With Regards > Palani Kannan. K > Office: +49-531-2616-226 > Mobile: +4917647098166Well, depending on your version of ActiveRecord (and assuming that your model is called Model) Rails 2: Model.find(:all, :select => ''column1'', :conditions => { :column2 => params[gm] }, :order => ''column1'') Rails 3: Model.select(''column1'').where(:column2 => params[gm]).order(''column1'').all But that will give you anemic and readonly instances of Model because they don''t have their id column. If you want fully formed Model instances, don''t specify the select part. If you just want the values of column1, you could do something like: Model.connection.select_values(Model.send(:sanitize_sql, ["SELECT DISTINCT column1 FROM #{Model.table_name} WHERE column2 = ? ORDER BY column1", params[gm])) But that''s obviously a bit ugly. Perhaps you should ask with REAL names from your application so that the answers can be made more naturally. -Rob Rob Biedenharn Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org http://AgileConsultingLLC.com/ rab-/VpnD74mH8+00s0LW7PaslaTQe2KTcn/@public.gmane.org http://GaslightSoftware.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-/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.
Try this: find_by_sql("SELECT column1 FROM table WHERE column2 = ? ORDER BY column1",params[:gm]) More here: http://api.rubyonrails.org/classes/ActiveRecord/Base.html#method-c-find_by_sql Walter On Sep 17, 2010, at 11:01 AM, PalaniKannan K wrote:> Dear all, > > I need to apply "find" instead of find_by_sql. Kindly suggest me. > Because, params[gm] is not working with find_by_sql. Kindly justify > whether my code below is in correct syntax... > > find_by_sql("SELECT column1 FROM table WHERE column2 = ''params[gm]'' > ORDER BY column1") > > -- > With Regards > Palani Kannan. K > Office: +49-531-2616-226 > Mobile: +4917647098166 > > > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > . > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en > .-- 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.
Dear Rob, I got it... Thank you for help... But, I applied code of Rails 2 in Rails 3, It worked for me. It may lead any problem with Rails 2 code in future? -- With Regards Palani Kannan. K -- 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 Sep 17, 2010, at 11:42 AM, PalaniKannan K wrote:> Dear Rob, > > I got it... Thank you for help... > > But, I applied code of Rails 2 in Rails 3, It worked for me. It may > lead any problem with Rails 2 code in future? > > -- > With Regards > Palani Kannan. KIt should work fine until Rails 3.1 (when the older ActiveRecord forms will be deprecated) -Rob Rob Biedenharn Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org http://AgileConsultingLLC.com/ rab-/VpnD74mH8+00s0LW7PaslaTQe2KTcn/@public.gmane.org http://GaslightSoftware.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-/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.
Dear Rob, Thank you for information. I will change all my Rails2 code to Rails3. It will be better in future. On 17 September 2010 17:49, Rob Biedenharn <Rob-GBZH0y1GwQfnZcttdmLDtcI/UQi/AW5J@public.gmane.org>wrote:> On Sep 17, 2010, at 11:42 AM, PalaniKannan K wrote: > > Dear Rob, > > I got it... Thank you for help... > > But, I applied code of Rails 2 in Rails 3, It worked for me. It may lead > any problem with Rails 2 code in future? > > -- > With Regards > Palani Kannan. K > > > It should work fine until Rails 3.1 (when the older ActiveRecord forms will > be deprecated) > > -Rob > > Rob Biedenharn > Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org http://AgileConsultingLLC.com/ > rab-/VpnD74mH8+00s0LW7PaslaTQe2KTcn/@public.gmane.org <Rob-/VpnD74mH8+00s0LW7PaslaTQe2KTcn/@public.gmane.org> > http://GaslightSoftware.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. >-- With Regards Palani Kannan. K Office: +49-531-2616-226 Mobile: +4917647098166 -- 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.