ruby on rails - Get record from url -


i know simple question guess brain , google-fu isn't working today.

let's have event, registrants, , can pay event using 1 or more payments.

i'm trying create payment linked registrant (who linked event).
payment should have both registrant_id , event_id.

my url looks this: (nested routes)

http://mysite.com/events/1/registrants/1/payments/new 

my controller looks like:

def create   @event = event.find(params[:event_id])   @registrant = registrant.find(:first, conditions: {id: params[:registrant_id], event_id: params[:event_id]} )    @payment = payment.new params[:payment] end 

i know there much better way it, i'm having trouble wording google :)

what syntax should using make .new automatically aware of event_id , registrant_id?

based on discussion in comments, there several ways question can addressed: direct way , rails way.

the direct approach creating objects related create object using new_object = classname.new suggested in question. take id of created object , set on existing object (directly existing_object.id = new_object.id or through other method if additional logic required). or set id on new object defining custom initializer, such as:

class payment   def initializer id_of_registrant     @registrant_id = id_of_registrant   end  ... end 

the advantage of approach allows assign registrant ids may come range of objects different classes, without having deal unnecessary or perhaps incorrect (for solution) inheritance , polymorphism.

the rails way, if have direct relationship (1 1) between registrant , 'mandatory' payment use has_many or belongs_to association, described in rails guide: http://guides.rubyonrails.org/association_basics.html

for example classes question:

class registrant < activerecord::base   has_one :payment end  class payment < activerecord::base   belongs_to :registrant end 

you want use appropriate migration create database tables , foreign keys go this. example:

class createregistrants < activerecord::migration   def change     create_table :registrants |t|       t.string  :name       t.timestamps     end      create_table :payments |t|       t.integer :registrant_id       t.string  :account_number       t.timestamps     end   end end 

of course, if registrants optionally make payment, or make multiple payments, need @ using has_many association.

with has , belongs associations, can nice things like:

 @payment.registrant = @registrant 

if have instantiated objects hand, or

 @payment.new(payment_amount)  @registrant = @payment.build_registrant(:registrant_number => 123,    :registrant_name => "john doe") 

if associations populated automatically.

the rails guide has plenty of examples, though in experience trying appropriate 1 actual use case show if there restrictions not anticipated. rails approach make future queries , object building easier, if have loose relationship model objects may find becomes restrictive or unnatural , equivalent associations better coded hand additional business rules.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

javascript - Clean way to programmatically use CSS transitions from JS? -

android - send complex objects as post php java -