views:

41

answers:

2

I'm very new to Ruby. I have the following piece of code which is giving me an error.

class ItemsController < ApplicationController
  def populate
    Location.all.each { |l|
      place = l.radar_place 
      next if !place 
      news_items = place.radars
      news_items.each { |ni|
        t = Lbs2.new  
        t.lat = l.lat
        t.lng = l.lng
        t.body = ni.body 
        t.save if !Lbs2.find_by_body(ni.body) #avoiding redundant news_items
      }
    }
    render :text => "Success"  
  end
end

The error being shown when I hit the url is NoMethodError in ItemsController#populate undefined method `lat=' for Lbs2 id: nil, body: nil, created_at: nil, updated_at: nil

This code worked perfectly without "if !Lbs2.find_by_body(ni.body)" statement. When I included this, in order to avoid the redundant news_items, it gave me the above mentioned error. Can someone please tell me how to get rid of this error at the same time avoiding redundant news_items from getting populated in Lbs2? Thanks in advance

A: 

to avoid redundant items i think you can overwrite the validate() method. something like the following:

def validate
   conditions = {"lat" => self.lat, "lng" => self.lng}
   if Lbs2.first(:conditions => conditions)
      errors.add_to_base "Your error message"
   end
end
j.
Thanks Jeff T. You were right. lat and lng field were not getting saved. The problem was datatype "double" is not supported in ruby. Datatype "float" has to be made use of. I dropped the Lbs2 and did ruby script/generate model Lbs lat:float lng:float body:text. Everything worked perfectly after this.
mamatha
A: 

OK, there are a few things going on here. The error message is saying that it doesn't know how to save something to the "lat" attribute of an Lbs2 object. Even though you told script/generate that you wanted a "lat" field for Lbs2, it looks like it didn't get saved after all. You can check db/schema.rb to be sure. But your first step will probably be to add that field to the appropriate table in the database. Read the Rails Migrations Guide for more info.

Also, there's a much better way to validate uniqueness of a field. In app/models/lbs2.rb (assuming your Lbs2 object is defined there), add a line (outside of any methods) that says:

validates_uniqueness_of :body

Read the Rails Validations Guide for more info.

Hope this helps,
-Jeff T.

Jeff Terrell
sorry if i'm wrong [i'm a beginner like @mamatha], but i think what i did is different of validates_uniqueness_of.if i want to have only one item with the same "lat" and "lng", but don't care about having many items with the same "lat" and different "lng", validates_uniqueness_of will do this for me?[sorry about my english too]
j.
Hi j., yes, you're validating the uniqueness of the *combination* of `lat` and `lng`, which is indeed different. I don't think you can do that using `validates_uniqueness_of`. @mamatha is manually validating the uniqueness of the `body` field alone, and I showed a better way to do that.
Jeff Terrell
tks, jeff. i don't know why i thought he wanted to validate more than one attibute =Panyway, thanks again for the explanation.
j.
You're welcome. I'm glad I could help. `:-)`
Jeff Terrell