user1224344
user1224344

Reputation: 129

Check if model exists even if it's not present

I'm stuck on something and have no idea what to do. In my reservations controller I have:

if Invitation.exists?

which returns true when '/events/1/invitations/2/reservations' is called, as expected, but returns:

'Couldn't find Invitation without an ID' 

when '/events/1/reservations' is called, also expected. I need a way to though to check if it exists when the 2nd url is called and in order to bypass the initiation if @invitation.

I'm very new to rails so any advice here would be welcome.

Thanks

---EDIT---

I think I may have given too little information.

Basically there are two URLs I want to use:

1st is example.com/events/1/invitations/2/reservations - in order to have the reservations index display all reservations that belongs to a given invitation, in this case '2'.

2nd is example.com/events/1/reservations - in order to display all reservations that belong to an event.

So in the reservations controller index action I have:

@event = Event.find(params[:event_id])
@reservations = Reservation.all

if Invitation.exists?
  @invitation = Invitation.find(params[:invitation_id])
  @invite = true
else
end

and in the reservations view index I have:

<% if @invite == true%>
  <% @invitation.reservations.each do |reservation| %>
    ...
<% else %>
  <% @event.reservations.each do |reservation| %>
    ...
<% end %>

So basically if the 2nd URL is used I can't use 'Invitation' in the controller action as it gives this error: "Couldn't find Invitation without an ID", so I need a way to tell the controllers if statement that if no invitation id is present (in the URL?) don't initiate @invitation.

Hope that's clearer, thanks for the help :)

Upvotes: 0

Views: 850

Answers (3)

Veraticus
Veraticus

Reputation: 16084

You're using a the model constant here instead of the actual instance of the model, which is what you're probably expecting. You want something more like:

@invitation = Invitation.find(params[:id])

And then continue going on if the invitation is found.

-- edit --

I think I get what you're trying to do here. Try this:

def index
    @event = Event.find(params[:id])
    @invitation = Invitation.find(params[:invitation_id]) if params[:invitation_id]
    @reserations = Reservation.all
    ...
end

Upvotes: 1

Harish Shetty
Harish Shetty

Reputation: 64373

The find method throw exception upon no data. You should use find_by_* method if you don't want this behavior, i.e.

@invitation = Invitation.find_by_id(params[:invitation_id])

Other issues with your code:

  • The Invitation.exists? call simply checks if there are any rows in invitations table. I am assuming that is not what you want. You have to supply an id/or conditions as input to check for specific object existence. Read the exists? documentation for more details.

  • I would refactor your code as follows:

Assuming this is your data model:

class Event
  has_many :reservations
  has_many :invitations
end

class Reservation
  belongs_to :event
  belongs_to :invitation
end

class Invitation
  belongs_to :event
  has_many :reservations            
end

Change your controller code:

@event = Event.find(params[:event_id])
@reservations = if params[:invitation_id].present?
  (@invitation = @event.invitations.find([:invitation_id])).reservations
else
  @event.reservations
end

Modify the view code to access the @reservations list:

<% @reservations.each do |reservation| %>
    ...
<% end %>

Upvotes: 0

Kyle Heironimus
Kyle Heironimus

Reputation: 8041

Your post isn't completely clear, but do you want this?

@invitation = params[:invitation] if params[:invitation]

Upvotes: 1

Related Questions