Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8778911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:38:45+00:00 2026-06-13T19:38:45+00:00

I am new to Ruby on Rails, and I would like to create a

  • 0

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
end


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

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])
    end

    #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]).first.id

    @citation = Citation.new(params[:citation])

  end

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 !

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T19:38:46+00:00Added an answer on June 13, 2026 at 7:38 pm

    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:

    autor.citations.create(params[:citation])
    

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to create a new model for a Ruby on Rails application.
I'm new in the world of Ruby on Rails. I have some problems to
I've only just started learning ruby on rails and I would like to create
I'm new to ruby/rails and trying to build a simple Projects / Tags app
I'm new with ruby and rails and have been struggling with this issue for
I am using Ruby on Rails 3 and I would like to change its
As part of a Rails project, I'd like to create some custom settings (specifically
I am running Ruby on Rails 3 and I would like to set up
I am running Ruby on Rails 3 and I would like to set up
I am using Ruby on Rails 3.0.9 and I would like to know what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.