Polymorphic problem

Hi, I’m in the process of learning ruby on rails so this will be one
of many posts by me when I can’t find the solution by googling.

I have set up a polymorphic relationship that I can’t get to work when
storing data.

def self.up
create_table :table_labels do |t|
t.column :label_type, :string
t.column :label_id, :integer
t.column :english, :text
t.timestamps
end

create_table :menus do |t|
t.column    :name,    :string,  :limit=>50
t.column    :parent_id,  :integer
t.column    :position,  :integer
t.column    :main,    :boolean
t.column    :footer,  :boolean
t.column    :active,  :boolean
t.column    :controller,:string,  :limit=>50
t.column    :action,  :string,  :limit=>50
  t.timestamps
end

end

class Menus < ActiveRecord::Base
acts_as_list
acts_as_tree
has_one :table_labels, :as => :label
validates_presence_of :name, :english, :parent_id
validates_uniqueness_of :name
end

class TableLabels < ActiveRecord::Base
belongs_to :label, :polymorphic => true
end

def add_menu
newmenu = Menus.new(:name => params[:label][:name], :parent_id =>
params[:label][:parent_id])
newlabel = TableLabels.new(:english => params[:label][:english])
newlabel.label = newmenu
newlabel.save!
render :action => :list_menus
end

What happens is a row is added to the table_labels but with a NULL
entry for label_id and nothing is added to menus table.

What am I missing?

can anyone help?

On Aug 21, 9:39 pm, almartin [email protected] wrote:

What happens is a row is added to the table_labels but with a NULL
entry for label_id and nothing is added to menus table.

Try saving the newmenu object before you assign it.

Fred

Tried that with newmenu.save and the same thing happens.
however if I use newmenu.save! I get the error English can’t be blank,
even though it isn’t

Shouldn’t rails be creating a JOIN in the sql? at the moment in the
trace is shows both tables being loaded seperately.
I’m sure it’s just me not yet understanding how table relationships
work.
Can any one help?

On Aug 22, 1:37 am, Frederick C. [email protected]

On 22 Aug 2008, at 18:20, almartin [email protected] wrote:

Tried that with newmenu.save and the same thing happens.
however if I use newmenu.save! I get the error English can’t be blank,
even though it isn’t

Well if there is a failing validation you need to fix it since rails
won’t save it

I fixed it!

The problem was so simple

I had the validation for the english field in the wrong class, it
should have been in the TableLabels class and not the Menus class.
works perfectly now.

Perhaps I need to explain how I wish this to work

I have a form on a page that adds a new menu item. the form consists
of the menu name, name in english (other languages will be added
later) and parent_id (from dropdown list of menu items).
When you submit I want the data to be added to the database like this

Menus.name = > post.name
Menus.parent_id => post.parent_id
TableLabels.english => post.english
TableLabels.label_id => Menus.id
TableLabels.label_type => Menus

How do I write the code to achieve this?

. If I wrote it in php - sql it would look like this.

$sql1 = “INSERT INTO menus (name, parent_id) VALUES (’”.
$_POST[‘name’]."’,".$_POST[‘parent_id’].")";
$sql2 = “INSERT INTO table_labels (menu_id,menu_type,english) VALUES
(”.mysql_insert_id().",‘menus’,’".$_POST[‘english’]."’)";

Anyone?

On Aug 22, 8:48 pm, Frederick C. [email protected]