Nidhin S G
Nidhin S G

Reputation: 1695

Saving to a particular column in database

I have a simple doubt.. I have a column named url in a table. And each row in a table is created using active admin. In that there is no provision to give url(It should be like that). It has got another column with Page_name. and with some calculations(shown below) i do have the required url in a variable. Now i want to update this column url in database with this variable as soon as it is created. this is what i have done so far.

class CustomPage < ActiveRecord::Base
  after_create :custom

  def custom
    @custom_page = CustomPage.all
    p "---------------------------------------"
    p @custom_page.last.id
    url = "/custom_page/"+@custom_page.last.page_name.split(" ").join("_")
    CustomPage.where(:id => @custom_page.last.id).update(:url => url)
  end
end

and i'm getting some kind of error. Wrong Number of Arguments (1 for 2).. I hope this is something very small.. thanks in advance.

Upvotes: 0

Views: 37

Answers (2)

Santhosh
Santhosh

Reputation: 29094

You could pass the id to update method like this

CustomPage.update(@custom_page.last.id, :url => url)

But in this particular case, you can do

@custom_page.last.update_attribute(:url, url)

Please note that these methods doesn't invoke validations. To do that, call

@custom_page.last.update_attributes(:url => url)

Upvotes: 1

Rajdeep Singh
Rajdeep Singh

Reputation: 17834

This line should look like this

CustomPage.where(:id => @custom_page.last.id).first.update(:url => url)

Since where returns ActiveRecord::Relation which is in form of a array so you need to fetch the object out of the array

Upvotes: 1

Related Questions