Closed GoogleCodeExporter closed 9 years ago
id method in rails must return primary_key. This is the rails code for id
method:
def id
attributes[self.class.primary_key]
end
You are overriding id method and breaking active scaffold
You can set another primary key instead of id, but you can't add another id
field.
Original comment by sergio.c...@gmail.com
on 10 Nov 2011 at 9:47
Hmm, its hard for me to explain, because english is not ny native tongue. Next
try:
Given this Table(bont blame me for this, it's an legacy app):
mysql> desc tbl_if;
+-------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+----------------+
| if_id | int(11) | NO | PRI | NULL | auto_increment |
| node_id | int(11) | YES | MUL | 0 | |
| bez | varchar(50) | YES | | NULL | |
| not_host_IF | tinyint(4) | YES | | 0 | |
| art | tinyint(4) | YES | | 0 | |
| MAC | varchar(20) | YES | | NULL | |
| vart | varchar(30) | YES | | NULL | |
| id-typ | varchar(20) | YES | | NULL | |
| id | varchar(80) | YES | | NULL | |
| connected | tinyint(4) | YES | | 0 | |
+-------------+-------------+------+-----+---------+----------------+
whith this content:
mysql> select * from tbl_if LIMIT 10;
+-------+---------+------+-------------+------+------+--------+--------+--------
-----------+-----------+
| if_id | node_id | bez | not_host_IF | art | MAC | vart | id-typ | id
| connected |
+-------+---------+------+-------------+------+------+--------+--------+--------
-----------+-----------+
| 1 | 260 | ALOM | 0 | 1 | NULL | Net-Cu | MAC | NULL
| 0 |
| 2 | 260 | ce0 | 0 | 1 | NULL | Net-Cu | MAC | NULL
| 0 |
| 3 | 260 | ce1 | 0 | 1 | NULL | Net-Cu | MAC | NULL
| 0 |
| 4 | 246 | eth0 | 0 | 0 | NULL | Net-Cu | MAC | NULL
| 0 |
| 5 | 247 | eth0 | 0 | 0 | NULL | Net-Cu | MAC | NULL
| 0 |
| 6 | 248 | eth0 | 0 | 0 | NULL | Net-Cu | MAC | NULL
| 0 |
| 7 | 249 | eth0 | 0 | 0 | NULL | Net-Cu | MAC | NULL
| 0 |
| 8 | 1 | eth0 | 0 | 0 | NULL | Net-Cu | MAC | NULL
| 0 |
| 11 | 262 | ce0 | 0 | 1 | NULL | Net-Cu | MAC |
00:03:ba:85:5c:21 | -1 |
| 12 | 262 | ce1 | 0 | 1 | NULL | Net-Cu | MAC |
00:03:ba:85:5c:22 | -1 |
+-------+---------+------+-------------+------+------+--------+--------+--------
-----------+-----------+
and this Model:
class Interface < MysqlDatabase
set_primary_key "if_id"
set_table_name "tbl_if"
belongs_to :system, :foreign_key => "node_id"
has_many :ips, :foreign_key => :if_id
def to_label
bez
end
end
active_scaffold fails with the routing error mentioned above. but if one
add this to the model:
def id
if read_attribute(:id) == nil then
"NULL"
else
read_attribute(:id)
end
end
active_scaffold renders the data. With one exception. I hav found no way to
display
the content of the field "id"
Jan
Original comment by jheb...@gmail.com
on 11 Nov 2011 at 6:20
I had understood you, but you cannot add id field, because adding id field you
break this ActiveRecord method:
def id
attributes[self.class.primary_key]
end
ActiveScaffold uses that rails method to get the "id" (if_id in your case) for
the record.
With your workaround you are confusing ActiveScaffold, which build routes using
the id field instead of primary key
Original comment by sergio.c...@gmail.com
on 11 Nov 2011 at 9:58
Original issue reported on code.google.com by
jheb...@gmail.com
on 10 Nov 2011 at 6:12