Hi, I am forced to use an old database with rails. Relevant db structure; mysql> select id,permission,incid from acl group by id; +----+------------+-------+ | id | permission | incid | +----+------------+-------+ | 1 | 58 | 1 | | 2 | 57 | 25 | | 3 | 57 | 32 | +----+------------+-------+ Show create table acl: CREATE TABLE `acl` ( id` int(4) NOT NULL, ..... `incid` int(11) NOT NULL auto_increment, PRIMARY KEY (`id`,`type`,`seq`,`incid`), UNIQUE KEY `incid` (`incid`) So the primary key that I want to use in place of id is "incid" "id" contains some acl ids and is not auto increment (has multiple rows returned per one `id`). My model looks like this: class Acl < ActiveRecord::Base set_table_name :acl; set_primary_key :incid; def self.primary_key() ''incid''; end self.primary_key = ''incid''; set_inheritance_column :ruby_id; end script/console>> r = Acl.find(2); <-- that''s checking for `incid`?> r.id => 2>>log: Acl Load (0.3ms) SELECT * FROM `acl` WHERE (`acl`.`incid` = 2) script/console>> r = Acl.find(31);?> r.id => 31 Acl Load (0.3ms) SELECT * FROM `acl` WHERE (`acl`.`incid` = 31) So, instead of r.id returning the value of the `id` field , it returns the value for the incid field. For the second example it should have returned `2` instead of `31` which is actually the value of `incid` mysql> select id,permission,incid from acl where incid=''31''; +----+------------+-------+ | id | permission | incid | +----+------------+-------+ | 2 | 57 | 31 | +----+------------+-------+ What should my model look like to be able to get the value of the `id` field? Dan
On Jun 30, 2009, at 10:29 AM, dan wrote:> Hi, > I am forced to use an old database with rails. > > Relevant db structure; > > mysql> select id,permission,incid from acl group by id; > +----+------------+-------+ > | id | permission | incid | > +----+------------+-------+ > | 1 | 58 | 1 | > | 2 | 57 | 25 | > | 3 | 57 | 32 | > +----+------------+-------+ > > Show create table acl: > > CREATE TABLE `acl` ( > id` int(4) NOT NULL, > ..... > `incid` int(11) NOT NULL auto_increment, > PRIMARY KEY (`id`,`type`,`seq`,`incid`), > UNIQUE KEY `incid` (`incid`) > > So the primary key that I want to use in place of id is "incid" > > "id" contains some acl ids and is not auto increment (has multiple > rows returned per one `id`). > > My model looks like this: > > class Acl < ActiveRecord::Base > set_table_name :acl; > set_primary_key :incid; > def self.primary_key() ''incid''; end > self.primary_key = ''incid''; > set_inheritance_column :ruby_id; > end > > > script/console >>> r = Acl.find(2); <-- that''s checking for `incid` > ?> r.id > => 2 >>> > > log: > Acl Load (0.3ms) SELECT * FROM `acl` WHERE (`acl`.`incid` = 2) > > script/console >>> r = Acl.find(31); > ?> r.id > => 31 > Acl Load (0.3ms) SELECT * FROM `acl` WHERE (`acl`.`incid` = 31) > > > So, instead of r.id returning the value of the `id` field , it returns > the value for the incid field. > > For the second example it should have returned `2` instead of `31` > which is actually the value of `incid` > > mysql> select id,permission,incid from acl where incid=''31''; > +----+------------+-------+ > | id | permission | incid | > +----+------------+-------+ > | 2 | 57 | 31 | > +----+------------+-------+ > > What should my model look like to be able to get the value of the `id` > field? > > DanSee if you can get the field that you want with this syntax: console> r=Acl.find(31); r[:id] r.id will always be the primary key (as you''ve discovered), but r[:id] (or r[''id''] if you prefer) will give you the ''id'' attribute. (I recently had to refer to a column ''type'' this way because ruby wants r.type to be the same as r.class, and r[:type] always returns the column.) -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org
> Hi, > I am forced to use an old database with rails. > > Relevant db structure; > > mysql> select id,permission,incid from acl group by id; > +----+------------+-------+ > | id | permission | incid | > +----+------------+-------+ > | 1 | 58 | 1 | > | 2 | 57 | 25 | > | 3 | 57 | 32 | > +----+------------+-------+ > > Show create table acl: > > CREATE TABLE `acl` ( > id` int(4) NOT NULL, > ..... > `incid` int(11) NOT NULL auto_increment, > PRIMARY KEY (`id`,`type`,`seq`,`incid`), > UNIQUE KEY `incid` (`incid`) > > So the primary key that I want to use in place of id is "incid" > > "id" contains some acl ids and is not auto increment (has multiple > rows returned per one `id`). > > My model looks like this: > > class Acl < ActiveRecord::Base > set_table_name :acl; > set_primary_key :incid; > def self.primary_key() ''incid''; end > self.primary_key = ''incid''; > set_inheritance_column :ruby_id; > end > > script/console>> r = Acl.find(2); <-- that''s checking for `incid` > > ?> r.id > => 2 > > > > log: > Acl Load (0.3ms) SELECT * FROM `acl` WHERE (`acl`.`incid` = 2) > > script/console>> r = Acl.find(31); > > ?> r.id > => 31 > Acl Load (0.3ms) SELECT * FROM `acl` WHERE (`acl`.`incid` = 31) > > So, instead of r.id returning the value of the `id` field , it returns > the value for the incid field. > > For the second example it should have returned `2` instead of `31` > which is actually the value of `incid` > > mysql> select id,permission,incid from acl where incid=''31''; > +----+------------+-------+ > | id | permission | incid | > +----+------------+-------+ > | 2 | 57 | 31 | > +----+------------+-------+ > > What should my model look like to be able to get the value of the `id` > field? > > Danjust use model.attributes[:id] or model.read_attribute :id