I have what I thought was a similar problem, but I am not sure what's
going wrong.
Here's my model:
class Bookmark < ActiveRecord::Base
has_many :tags, :through => :bookmarks_tags
end
(in file app/models/bookmark.rb)
class Tag < ActiveRecord::Base
has_many_and_belongs_to_many :bookmarks, :through => :bookmarks_tags
end
(in file app/models/tag.rb)
class BookmarksTag < ActiveRecord::Base
belongs_to :bookmark
belongs_to :tag
end
(in file app/models/BookmarksTag.rb)
My database looks like this:
CREATE TABLE `bookmarks` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
`url` varchar(255) default NULL,
`created_at` datetime default NULL,
`updated_at` datetime default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;
-- --------------------------------------------------------
--
-- Table structure for table `bookmarks_tags`
--
CREATE TABLE `bookmarks_tags` (
`bookmark_id` int(11) default NULL,
`tag_id` int(11) default NULL,
`created_at` datetime default NULL,
`updated_at` datetime default NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Table structure for table `schema_migrations`
--
CREATE TABLE `schema_migrations` (
`version` varchar(255) NOT NULL,
UNIQUE KEY `unique_schema_migrations` (`version`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Table structure for table `tags`
--
CREATE TABLE `tags` (
`id` int(11) NOT NULL auto_increment,
`tagword` varchar(255) default NULL,
`created_at` datetime default NULL,
`updated_at` datetime default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
I'm getting this error in my "show" view:
Could not find the association :bookmarks_tags in model Bookmark
Extracted source (around line #13):
10:
11: <p>
12: <b>Tags:</b>
13: <% for tag in @bookmark.tags %>
14: <%= tag.tagword %>
15: <% end %>
16:
If I change bookmark.rb as follows it works, but I would like to learn
what I've done wrong for future reference.
class Bookmark < ActiveRecord::Base
has_and_belongs_to_many :tags
end
Any advice would be greatly appreciated.
Regards,
Andrew.
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---