sql - The includes and where doesn't retrieve the correctly associated object -
i'm trying implement search feature on project, add class method :
class donation < activerecord::base belongs_to :member def self.search_by_name name includes(:member).where(member: { name: name } ) end end
and call in donatinos#index
class donationscontroller < applicationcontroller def index search_donations end private def search_donations if !params[:name].blank? @donations = donation.search_by_name params[:name] else @donations = donation.all end end end
finally, implement index template:
<%= form_tag donations_path,:method => 'get' %> <%= text_field_tag :name %> <%= submit_tag "search", name: nil %> <% end %> <table> <% @donations.each |donation| %> <tr> <td><%= donation.total %></td> </tr> <% end %> </tbody> </table>
i got error message after take "ted wang" in form
sqlite3::sqlexception: no such column: member.name: select "donations"."id" t0_r0, "donations"."organization_id" t0_r1, "donations"."category" t0_r2, "donations"."borrow_id" t0_r3, "donations"."member_id" t0_r4, "donations"."total" t0_r5, "donations"."note" t0_r6, "donations"."created_at" t0_r7, "donations"."updated_at" t0_r8, "donations"."time" t0_r9, "members"."id" t1_r0, "members"."no" t1_r1, "members"."name" t1_r2, "members"."nick_name" t1_r3, "members"."introducer" t1_r4, "members"."role" t1_r5, "members"."gender" t1_r6, "members"."birthday" t1_r7, "members"."lunar_birthday" t1_r8, "members"."lunar_deathday" t1_r9, "members"."identified_number" t1_r10, "members"."home_phone" t1_r11, "members"."company_phone" t1_r12, "members"."bbcall" t1_r13, "members"."mobile" t1_r14, "members"."address" t1_r15, "members"."permanent_address" t1_r16, "members"."education" t1_r17, "members"."job" t1_r18, "members"."created_at" t1_r19, "members"."updated_at" t1_r20, "members"."organization_type" t1_r21, "members"."organization_id" t1_r22, "members"."involved" t1_r23 "donations" left outer join "members" on "members"."id" = "donations"."member_id" "member"."name" = 'ted wang'
did use includes
wrong? , member has attribute name, ran db:migrate.
as per rails convention, table names lower cased , pluralized model names in clause should specify members
(in plural) , not member
(in singular).
you can debug looking @ error message: sqlite3::sqlexception: no such column: member.name
. error message, 1 can diagnose table name members
in query wrong referring member.name
instead of members.name
.
def self.search_by_name name includes(:member).where(members: { name: name } ) ## ^ ## plural end
Comments
Post a Comment