0

In my application I want to check to see whether or not a concert model exists with the same artist and date fields as a review model. If It does I want to add the review to the concert, if not then I want to create a new concert and review, because concert has_many reviews and reviews belongs_to concert...

So I wrote an exists? function in my concert controller:

def exists(@artist, @date)?
    @concert_exists = @concerts.find_by_artist_and_date(artist: @artist, date: @date)
    if @concert_exists.nil?
        return false
    else
        return true
    end
  end

and then in my review controller I'm trying to do this for its create function:

def create

    if Concert.exists(review_params[:artist], review_params[:date])?
      #add review to this concert 
    else
      @concert = Concert.create(:artist => "artist", :venue => "venue", :date => "2014-2-2")
      @review = @concert.reviews.create(review_params) 
      @concert.artist = @review.artist
      @concert.venue = @review.venue
      @concert.date = @review.date
      @concert.save
    end
end

I keep getting an error that says

"syntax error, unexpected keyword_else"

Is my implementation incorrect and is there an easier way to do what I'm attempting?

tshepang
  • 12,111
  • 21
  • 91
  • 136
parameter
  • 894
  • 2
  • 18
  • 35

2 Answers2

1

Rails already has a method that does that for you find_or_create_by so you don't need to re-invent the wheel

@concert = Concert.find_or_create_by(artist: @artist, venue: "venue", date: @date)
@concert.reviews.create(review_params)
bjhaid
  • 9,592
  • 2
  • 37
  • 47
  • so based on this, how could I check to see whether or not a new concert was created or not? – parameter Apr 07 '14 at 00:13
  • @user2739431 the rails documentation says: The `find_or_create_by` method checks whether a record with the attributes exists. If it doesn't, then create is called, you might as well read the documentation to have a better understanding of what it does, you can also include `venue: "venue"` in the hash you are passing to the method – bjhaid Apr 07 '14 at 00:16
  • right, but I need to know whether or not it is created so then I can add a review to one that already exists – parameter Apr 07 '14 at 00:17
  • @user2739431 I have updated my answer to reflect exactly what you want – bjhaid Apr 07 '14 at 00:19
  • @user2739431 is it saving `@concert`?? – bjhaid Apr 07 '14 at 00:36
  • it's weird, when I do "@concert = Concert.find_or_create_by(artist: review_params[:artist], venue: review_params[:venue], date: review_params[:date])" I get that error, but if i do something to test it like @concert = Concert.find_or_create_by(artist: review_params[:artist], venue: "asdf", date: "2012-2-2") " i don't get the error... – parameter Apr 07 '14 at 00:40
  • and yes it saves both the concert and review @bjhaid – parameter Apr 07 '14 at 00:41
  • @user2739431 use `find_or_create_by!` with the bang, `ActiveRecord` would raise an exception that would be descriptive of why the `Concert` object is not being created – bjhaid Apr 07 '14 at 00:43
  • and it is also creating a new concert even when I use the same artist venue and date as one that already exists. – parameter Apr 07 '14 at 00:44
  • @user2739431 I think you probably don't want the date in the hash, as that might be causing the issue, you should also take a careful look at the query `ActiveRecord` is generating, that would give you a hint – bjhaid Apr 07 '14 at 00:45
  • "Date can't be blank" I'm doing (artist: review_params[:artist], venue: review_params[:venue], date: review_params[:date]) i also tried :date => review_params[:date] same problem though – parameter Apr 07 '14 at 00:46
  • how do you expect `date` to be generated as you can see `review_params[:date]` is blank and that seems to be causing your issues – bjhaid Apr 07 '14 at 01:06
  • yes it is blank, however artist and venue are not blank, so it must be an issue with referencing a date field. thank you for all your help I will ask this issue in a separate question, you've pointed me in the right direction – parameter Apr 07 '14 at 01:08
0

I think you want to write:

def exists?(@artist, @date)

and:

if Concert.exists?(review_params[:artist], review_params[:date])

Change the location of the question mark.

Lencho Reyes
  • 359
  • 3
  • 10
  • you're right that helped, but now i get "wrong number of arguments (2 for 0..1)" for the line if Concert.exists?(review_params[:artist], review_params[:date]) – parameter Apr 07 '14 at 00:10