relational database - Rails 5 Active Record Associations -
i learning how work ruby on rails , want find best way solve issue. have searched through many forums , rails documentation, haven't been able understand solution yet, hoping might able help!
my app has 3 controllers- users, user_addresses, , invoices. each user can have multiple user_addresses, , multiple invoices. added user_id column primary key user_addresses table , user_id , address_id column primary keys in invoices table.
when create invoice , select address assign to, save it, cannot figure out how populate both user_id , address_id, can 1 or other save. in example below getting user_id save, not sure both user_id , id address save invoice.
i have user model set has_many :user_addresses , has_many :invoices. both invoice , useraddress model belongs_to :user. have read, think may need make use of :through extension, lost , not sure do. or guidance appreciated!
invoices form
<div class="form-group"> <%= label_tag(:user_id, "customer") %> <%= f.select(:user_id, @addresses.map {|a| [a.user.first_name + ' ' + a.user.last_name + ' - ' + a.street_1, a.user.id]},{}, {:class => 'form-control'}) %> </div> <div class="form-check"> <label class="form-check-label"> <%= f.check_box(:status, :class => 'form-check-input') %> status </label> </div> <div class="form-group"> <%= label_tag(:service_date, "service date") %> <%= f.text_field(:service_date, 'data-provide' => 'datepicker', :class => 'form-control') %> </div> <div class="form-group"> <%= label_tag(:service_type, "service type") %> <%= f.select(:service_type, ["general pest", "weed control", "specialty pest", "rodent"],{}, {:class => 'form-control'}) %> </div> <div class="form-group"> <%= label_tag(:service_charge, "service charge") %> <%= f.text_field(:service_charge, :class => 'form-control') %> </div>
invoices controller
def new @invoice = invoice.new @addresses = useraddress.all end
invoices table
create_table "invoices", force: :cascade, options: "engine=innodb default charset=latin1" |t| t.integer "user_id" t.integer "address_id" t.boolean "status", default: false t.string "service_date" t.string "service_type" t.string "service_areas" t.string "service_materials" t.string "service_charge" t.string "service_info" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["address_id"], name: "index_invoices_on_address_id", using: :btree t.index ["user_id"], name: "index_invoices_on_user_id", using: :btree
end
i added user_id column primary key user_addresses table
declaring pk says user can have @ 1 address. if want user able have multiple addresses don't want pk. want each user-address pair unique/pk.
user_id , address_id column primary keys in invoices table
this means can't have 2 invoices same person-address pair. that's not want. typically candidate/primary key invoices unique runtime invoice id. although it's quite want some kind of index optimize finding invoices based on person-address pairs.
if declare columns candidate key (possibly pk) saying unique , not null. want invoice without having both user , address, contradicts that. there many invoices per user, because there many user-address pairs (each invoice) per user. declare such pair has invoice. , not user does, because have keep userinvoice redundantly in sync invoice. (but want invoice ids.)
a :through manages redundancy. including addressing indexing optimization mentioned above.
read first making relational design expressing (and desired optimizations) via active record design.
Comments
Post a Comment