Tuesday, 15 January 2013

ruby - Rails checking db for already existing user and add comment without creating a new user -


I have a landlord and comment class I landlord's 1: N comments when a landlord becomes, So he makes a comment on the same form (nested).

When the form is submitted, users_controller is called # create.

I want to check the database, the landlord with the same name, city and state already exists and instead add a comment to that landlord instead of creating a new one.

  def create #check If the landlord of the same name already exists and add a comment that if the house-lord.find_b_name (param [: name]) & amp; Amp; Landlord.find_by_city (parameter [: city]) & amp; & Amp; Landlord.find_by_province (param [: province]) @landlord_exists = landlord.find_b_name (param [: name]) & amp; Amp; Landlord.find_by_city (parameter [: city]) & amp; & Amp; Landlord.find_by_province (params [: province]) @landeldord_ axis Com.build @ landlord_exists.comments [0] .setIP request.remote_ip @ landlord_exists.save else @landlord = landlord.New (consultation [[landlord]) @localist. Comments [0] .setIP request.remote_ip If @ landlord.save flash [: success] = "Thank you for sending a landlord"   

at the end of the second term of Landlords # 1 below

  def @landlord = Landlord.where (: name => params [: name],: city => params [: city],: province = & Gt; the ultimate [: province]) .first_or_create! @hotelowner comrade [0] .setIP request.remote_ip if the @   

line

  from the owners of the land for the invitation from the landlord @landlord = Landlord where (: name => param [[name],: city = & gt; params [: city],: province => parameter [: province]). First_or_create!   

It seems that set options are called, thus throwing errors is to return the zero object. What would be the reason? I tried to do this in the terminal and it worked fine, though I am using hardcod values.

The easiest way to do this is to ActiveRecord :: Relation's first_or_create! must use the method. This will execute a query based on the parameter entered in the form, and if no match is found, then create a record, which means that you can get the if ... else conditionals:

  @landlord = Landlord. Where (settlement [: landlord]). @lossowner.com.bild # etc ...    

No comments:

Post a Comment