How to access a model from a different controller

25 views
Skip to first unread message

Ruth Stephenson

unread,
Jul 4, 2016, 7:39:17 AM7/4/16
to rubyonra...@googlegroups.com
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/.

Colin Law

unread,
Jul 4, 2016, 8:31:22 AM7/4/16
to Ruby on Rails: Talk
On 4 July 2016 at 12:38, Ruth Stephenson <li...@ruby-forum.com> wrote:
> 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)

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

Ruth Stephenson

unread,
Jul 4, 2016, 8:37:12 AM7/4/16
to rubyonra...@googlegroups.com
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.

Colin Law

unread,
Jul 4, 2016, 8:46:00 AM7/4/16
to Ruby on Rails: Talk
On 4 July 2016 at 13:36, Ruth Stephenson <li...@ruby-forum.com> wrote:
> 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

Ruth Stephenson

unread,
Jul 5, 2016, 7:52:56 AM7/5/16
to rubyonra...@googlegroups.com
Colin Law wrote in post #1184429:
Sorry! I actually wasn't aware this was a mailing list. I will quote the
previous message from now on. Thank you for your suggestion. I will try
that.
Reply all
Reply to author
Forward
0 new messages