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=?’)
…
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
…