hey guys,i follow the Ruby On Rails Guides,but i didn''t see the query about the guide,so i have a question: name and title are attrs in posts,i need to find records by query name and title with any of them are none-empty so should i need to detect the name or title such as: if name!=nil and title==nil then Post.where(''name=?'') elsif name==nil and title!=nil then Post.where(''title=?'') elsif(name!=nil and title!=nil) then Post.where(''name=? and title=?'') so much boring codes,isn''t it? i want to Post.clever_find(@post) -- 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.
Hi dan, You could do something like: ... posts = [] if not name.blank? or not title.blank? sql_buf = [] vals_buf = [] if not name.blank? sql_buf << "name=?" vals_buf << name end if not title.blank? sql_buf << "title=?" vals_buf << title end conditions = [sql_buf.join(" and ")] + vals_buf posts = Posts.where(conditions).order(...)... end return posts ... Jeff On Jan 28, 12:59 am, dan <cmaj...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> hey guys,i follow the Ruby On Rails Guides,but i didn''t see the query > about the guide,so i have a question: > name and title are attrs in posts,i need to find records by query name > and title with any of them are none-empty > so should i need to detect the name or title such as: > if name!=nil and title==nil then > Post.where(''name=?'') > elsif name==nil and title!=nil then > Post.where(''title=?'') > elsif(name!=nil and title!=nil) then > Post.where(''name=? and title=?'') > > so much boring codes,isn''t it? > > i want to Post.clever_find(@post)-- 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 this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jan 28, 2:25 pm, Jeff Lewis <jeff.bu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> conditions = [sql_buf.join(" and ")] + vals_buf > posts = Posts.where(conditions).order(...)...Or Post.where({:name => name, :title => title}.reject {|k,v| v.blank?}) Fred> end > return posts > ... > > Jeff > > On Jan 28, 12:59 am, dan <cmaj...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > hey guys,i follow the Ruby On Rails Guides,but i didn''t see the query > > about the guide,so i have a question: > > name and title are attrs in posts,i need to find records by query name > > and title with any of them are none-empty > > so should i need to detect the name or title such as: > > if name!=nil and title==nil then > > Post.where(''name=?'') > > elsif name==nil and title!=nil then > > Post.where(''title=?'') > > elsif(name!=nil and title!=nil) then > > Post.where(''name=? and title=?'') > > > so much boring codes,isn''t it? > > > i want to Post.clever_find(@post)-- 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 this group at http://groups.google.com/group/rubyonrails-talk?hl=en.