Jump to content

The ultimate community for Ruby on Rails developers.


Photo

rails activerecord reference a date field within form with find_or_create_by

date field findby createby associations

  • Please log in to reply
4 replies to this topic

#1 nick5745

nick5745

    Passenger

  • Validating
  • 4 posts

Posted 07 April 2014 - 01:53 AM

I'm having some issues when using find_or_create_by in my rails application. I have a Concert model that has_many Reviews, I want to create a new concert if the review has an artist, venue, and date that doesn't already exist within another concert, but when I try to do the following I get an error.

within my Review controller I check to see if there's a Concert that has these datafields from the Review form:

 

 

def create
    @concert = Concert.find_or_create_by(artist: review_params[:artist], venue: review_params[:venue], date: review_params[:date])
    @review = @concert.reviews.create(review_params)
end

 

 

Page says:

 

 

You cannot call create unless the parent is saved

 

After hitting submit with artist: "testArtist", venue: "testVenue" and date: "2014-4-6", console says:

 

 

Processing by ReviewsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"dhwcJVZk+gDaAUVpSOMjiAIukhZXCQF9Qz0diYmnOns=", "review"=>{"artist"=>"testArtist", "venue"=>"testVenue", "date(1i)"=>"2014", "date(2i)"=>"4", "date(3i)"=>"6", "genre"=>"Electronic", "sound"=>"1", "stagePresence"=>"1", "songSelection"=>"1", "overallRating"=>"1", "comments"=>""}, "commit"=>"Create Review"} Concert Load (2.8ms) SELECT "concerts".* FROM "concerts" WHERE "concerts"."artist" = 'testArtist' AND "concerts"."venue" = 'testVenue' AND "concerts"."date" IS NULL LIMIT 1

 

 

Strangely when I change my code to manually look for a date, it works, and a concert and review are successfully created.

 

 

def create
    @concert = Concert.find_or_create_by(artist: review_params[:artist], venue: review_params[:venue], date: "2014-4-6")
    @review = @concert.reviews.create(review_params)
end

 

Is there a different convention that needs to be used when referencing a field that is a date?



#2 stevieing

stevieing

    Dispatcher

  • Members
  • 40 posts

Posted 07 April 2014 - 01:35 PM

Your problems is this date: review_params[:date] which is null because you are passing the date as year, month and day i.e. "date(1i)"=>"2014", "date(2i)"=>"4", "date(3i)"=>"6".

 

You need to combine these fields and pass as a single parameter.

 

Hope that helps

 

Regards

 

Steve.



#3 nick5745

nick5745

    Passenger

  • Validating
  • 4 posts

Posted 08 April 2014 - 12:29 AM

Your problems is this date: review_params[:date] which is null because you are passing the date as year, month and day i.e. "date(1i)"=>"2014", "date(2i)"=>"4", "date(3i)"=>"6".

 

You need to combine these fields and pass as a single parameter.

 

Hope that helps

 

Regards

 

Steve.

 

How would I go about doing that? Convert to string then convert back to a date? or is there an easier way?



#4 nick5745

nick5745

    Passenger

  • Validating
  • 4 posts

Posted 08 April 2014 - 12:52 AM

I tried doing this here: http://api.rubyonrai...-to_formatted_s

 

.to_formatted_s(:db) but I get a no method error for NilClass



#5 stevieing

stevieing

    Dispatcher

  • Members
  • 40 posts

Posted 08 April 2014 - 08:32 AM

I don't know whether you need to do that. From what I understand ActiveRecord will convert your parameters and you do not need to name them explicitly.

 

Try:

 

@concert = Concert.find_or_create_by(review_params)

@review = @concert.reviews.create(review_params)

 

Steve.







Also tagged with one or more of these keywords: date field, findby, createby, associations

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users