how to get the id from an object stored in a database in Ruby

I am new to Ruby on Rails, and I would like to create a very simple website where I can save some citations.
I have several tables: Citation and Autor, here are the corresponding models.

class Citation < ActiveRecord::Base
  belongs_to :autor
  attr_accessible :text, :autor_id

class Autor < ActiveRecord::Base
  has_many :citations
  attr_accessible :name

I would like that when an user create a new citation that bellow to a new autor, the autor has to be automatically created.

Here is a part of my citations_controller:

def create
    # Here I check if the autor already exist or not, if not, I create him (The user gave me his name)
    if !(Autor.exists?(:name => params[:citation][:autor_id]))
      Autor.create(:name => params[:citation][:autor_id])

    #As I only have the name of the autor, I try to retrieve his Id
    params[:autor_id] = Autor.where(:name => params[:citation][:autor_id])

    @citation =[:citation])


The point is, when I create a new citation, the field autor_id is filled with a 0 not with the correct ID of the autor. I think the mistake is when I try to retrieve the Id from the name, but I don't know how to fix it, and perhaps there is a simpler solution !

Thank you !

while creating a new Autor save it in variable, then use this variable to get id
You can use this as

autor = (temp = Autor.where(:name => params[:citation][:autor_id]).first ? temp : Autor.create(:name => params[:citation][:autor_id])

You can also use find_or_create method of rails which does the same as above.

After that if you want to create a citation you can also do:


this will create a citation with autor id and params[:citation] as its attributes

Related Articles
  • I am seeking an android solution to calculate the distance to an object and then determine the size (height, width, length). Example would be a bridge, building. This information is not available in a single image. It would be possible using stereosc
  • Hi in my application I am getting image urls and video urls from service. After getting these from service, i am displaying the images in gridview but for video urls i am not able to get the thumbnails. how to get the thumbnails for this url.. exampl
  • I am using the twilio meteor package: It works well but I cannot get the result from my method call in the client callback. Whatever I try, the result from the call is always undefined. I tried using Fu
  • My application has many pages inside Radwindow. in code behind of the page, how can I get the Radwindow. eg: I want to change Radwindow's title,... Thanks for any answer you can try var wnd = RadWindow.GetParentRadWindow(this);
  • I'm trying to get the charset attribute in any HTML meta tag. (ie.< meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" >) Is there any way to do that in C++ under linux. I was using HTML tidy as a parser but I
  • In Google Chrome browser, i have tried several way + following and none is giving me the value of the file name which was attached, after validation i will submit the file. But always its undefined or val() does not found.. How to resolve it? console
  • Below is the information I receive from DB. It's originally from an XML file, but this is stored in DB table and I can't change the situation. I need to work these way, since there is no more information available. I read these in a dataset and my ro
  • Strings in resource files have a name, a value and a comment The ResXResourceReader class gives me access to the name and the value. Is there a way to read the comment? You should be able to get Comment via ResXDataNode class:
  • how to get the value from HTML to svg file. (ie) i have to fix on one text box and command button in html then click the button the values on goes to SVG and print that text. My code is here. what type of mistake (or) error in my code. **In html file
You Might Also Like