Ruby on Rails Monday, July 4, 2016

I've been stuck with this for days. I think I should be able to access a
variable in a controller of one model, that is stored in another model
easily?

I have four models: User - (1-1) - Profile - (1-M) - Appointment - (1-1)
- Option.

Options contains pricePerPerson and discount for an appointment.
Appointments contains, among others, numPeople. I want to access the
figure in the Options model for the pricePerPerson (and discount, if
appropriate) and use it in the view show for Appointments. (@ <%=
appointment.price = Option.pricePerPerson * numPeople %>)

So far I haven't been able to do this and have had to resort to
hard-coding a price per person. I would rather not hard-code that, if
possible.

I thought I could do this either of the two ways:

Please can someone tell me how I might go about solving this without
deleting any tables. I want to keep the database as is if possible.

1: as there is a belongs_to relationship between options and
appointments - (Options belongs to appointments)

Appointments Controller:

def new
@appointment = Appointment.new
@appointment.price = option.appointment.pricePerPerson
end
appointments/show.html.erb:

<p>
<strong>Price:</strong>
<%#= 5 * @appointment.numpeople %>
<%= @appointment.price * @appointment.numpeople %>
</p><br>
2: Something like this:

Appointments Controller:

def new
@appointment = Appointment.new
pricePerPerson = params([:appointment])
price = Option.find(pricePerPerson)
@appointment.price = price.pricePerPerson
end

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/c99a7887d23ddd56e6235b29d3cdecdd%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment