I have list.rb and item.rb. The first has a has_many relationship
with items. The second belongs to list. The sources are below:
class Item < ActiveRecord::Base
belongs_to :list
belongs_to :author
end
class List < ActiveRecord::Base
has_many :items, :dependent => true, :order => ''created_at
DESC''
belongs_to :author
validates_presence_of :title
validates_uniqueness_of :title
end
Here is the SQL schema:
CREATE TABLE `authors` (
`id` INT NOT NULL auto_increment,
`name` VARCHAR(32) DEFAULT NULL,
`username` VARCHAR(32) DEFAULT NULL,
`password` VARCHAR(32) DEFAULT NULL,
`address` VARCHAR(64) DEFAULT NULL,
`created_at` TIMESTAMP NOT NULL,
`updated_at` TIMESTAMP NOT NULL,
PRIMARY KEY (`id`)
) ENGINE = MyISAM;
CREATE TABLE `authors_lists` (
`author_id` INT NOT NULL,
`list_id` INT NOT NULL
) ENGINE = MyISAM;
CREATE TABLE `lists` (
`id` INT NOT NULL auto_increment,
`title` TEXT,
`description` TEXT,
`created_at` TIMESTAMP NOT NULL,
`updated_at` TIMESTAMP NOT NULL,
PRIMARY KEY (`id`)
) ENGINE = MyISAM;
CREATE TABLE `items` (
`id` INT NOT NULL auto_increment,
`name` VARCHAR(16) DEFAULT NULL,
`completed` BOOLEAN DEFAULT ''0'',
`created_at` TIMESTAMP NOT NULL,
`updated_at` TIMESTAMP NOT NULL,
PRIMARY KEY (`id`)
) ENGINE = MyISAM;
CREATE TABLE `sessions` (
`id` INT NOT NULL auto_increment,
`sessid` CHAR(32) NOT NULL,
`data` TEXT,
`created_at` TIMESTAMP NOT NULL,
`updated_at` TIMESTAMP NOT NULL,
PRIMARY KEY (`id`),
INDEX session_index (sessid)
) ENGINE = MyISAM;
Anything look weird? Thanks for the help!
Cheers,
Eric Czarny
>> I am trying to create a todo list application for class, but I am
>> having trouble listing each item in a list. Currently I find the
>> current list being viewed by id, then I attempt to gather each each
>> item in the list with: @items = @list.find_all_in_items(). When I
>> try to load the page though I get the following exception:
>>
>> Unknown column ''items.list_id'' in ''where
clause'': SELECT * FROM
>> items WHERE items.list_id = 1 ORDER BY created_at DESC
>>
>> I have NO idea why Rails is looking for an the list_id in the items
>> table. I made extra sure my models were correct, and each
>> relationship looks fine. Any ideas?
>>
>
> What do your relations look like?
> Also, what do the has_many and belongs_to relationships look like?