How to access a model from a different controller

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

In that case if you have appointment, say @appointment, and it has an option that it belongs to then you can say @appointment.price = @appointment.option.pricePerPerson * numPeople but you should not do this as that means you are saving (effectively) the same information in two places in the database, instead define an access method in appointment.rb that defines the method price() to return self.option.pricePerPerson*numPeople.

Also, it advise sticking to the rails conventions on naming (so price_per_person etc). Otherwise you will find that some of the rails magic may not work.

Further, I suggest again (I believe I have previously suggested that you do this) that you work right through railstutorial.org, including the exercises, in order to get an understanding of the basics of rails.

Colin

I have been reading them, but as yet haven't understood what I'm doing wrong. But thank you for your comment. I will make a price method and keep reading too.

Remember this is a mailing list, not a forum (though you may be accessing it via a forum-like interface). Because you have not quoted the previous message no-one reading this will know what you are talking about without looking for the previous message.

Don't just read it. Work through it so that you end up with a working site. You will gain a huge amount from trying to work out where you have made typos and so on.

In your price method don't forget to allow for the case where you have not yet assigned an option to the appointment.

Colin

Colin Law wrote in post #1184429: