# Polymorphic problem

**URL:** <https://www.ruby-forum.com/t/polymorphic-problem/146523>\
**Category:** Rails\
**Created:** [August 21, 2008, 10:51pm UTC](https://www.ruby-forum.com/t/polymorphic-problem/146523 "2008-08-21T22:51:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![almartin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d6d6ee/32.png) [@almartin](https://www.ruby-forum.com/u/almartin)\
**Post date:** [August 21, 2008, 10:51pm UTC](https://www.ruby-forum.com/t/polymorphic-problem/146523/1 "2008-08-21T22:51:48Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![almartin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d6d6ee/32.png) [@almartin](https://www.ruby-forum.com/u/almartin)\
**Post date:** [August 22, 2008, 2:38am UTC](https://www.ruby-forum.com/t/polymorphic-problem/146523/2 "2008-08-22T02:38:19Z")

</div>

On Aug 21, 9:39 pm, almartin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![almartin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d6d6ee/32.png) [@almartin](https://www.ruby-forum.com/u/almartin)\
**Post date:** [August 22, 2008, 7:20pm UTC](https://www.ruby-forum.com/t/polymorphic-problem/146523/3 "2008-08-22T19:20:50Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![almartin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d6d6ee/32.png) [@almartin](https://www.ruby-forum.com/u/almartin)\
**Post date:** [August 22, 2008, 9:49pm UTC](https://www.ruby-forum.com/t/polymorphic-problem/146523/4 "2008-08-22T21:49:09Z")

</div>

On 22 Aug 2008, at 18:20, almartin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![almartin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d6d6ee/32.png) [@almartin](https://www.ruby-forum.com/u/almartin)\
**Post date:** [August 23, 2008, 12:07am UTC](https://www.ruby-forum.com/t/polymorphic-problem/146523/5 "2008-08-23T00:07:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![almartin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d6d6ee/32.png) [@almartin](https://www.ruby-forum.com/u/almartin)\
**Post date:** [August 22, 2008, 11:08pm UTC](https://www.ruby-forum.com/t/polymorphic-problem/146523/6 "2008-08-22T23:08:18Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
