Reputation: 3281
hello i was wondering if it was possible to retrieve the :id (the primary key that auto-increments in the db) after it gets saved? i have in my favorite_relationships controller
def create
@lesson = Lesson.find(params[:favorite_relationship][:lesson_id])
current_user.favorite!(@lesson)
#params[:id] = ...
respond_to do |format|
format.html { redirect_to @lesson }
format.js
end
end
when current_user.favorite! gets called, it goes to my user model
def favorite!(lesson)
favorite_relationships.create!(lesson_id: lesson.id)
end
however, is there a way to retrieve the :id the favorite_relationship is getting saved as? i wanted to set it in the commented out line
#params[:id] =
i need to specify a params[:id] for a gem im using
thanks
Upvotes: 0
Views: 696
Reputation: 1870
copy from railsforum
say your table name is table_A
@a = table_A.new()
@a.first = "firs name"
@a.last = "last name"
@a.save
To GET LAST INSERT ID OF table_A just use @a.id
@a.id will return the last insurted id of table_A
Upvotes: 1
Reputation: 2014
I assume you are looking for favorite_relationships.id? in this case modify you controller to strore the value in the variable as
favorite_relationships = current_user.favorite!(@lesson)
params[:id]= favorite_relationships.id
Upvotes: 1