RoR——模型关系
您好,有一个名为category的表,它有两个表user的外键,结构如下。如何使用关系获取created_by和modified_by对应的用户名。 @category = Category.find(params[:id])
仅提供类别表中的详细信息。我当前的模型类是
class Category < ActiveRecord::Base
validates_uniqueness_of :title, :message => "Title already exist"
validates_presence_of :title, :message => "Cannot be blank"
belongs_to :user
end
如何将创建的字段和修改的字段关联到用户模型
CREATE TABLE IF NOT EXISTS `categories` (
`id` int(11) unsigned NOT NULL auto_increment,
`title` varchar(255) NOT NULL,
`created_by` int(11) unsigned default NULL,
`modified_by` int(11) unsigned default NULL,
`created` datetime NOT NULL,
`modified` timestamp NOT NULL default CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `created_by` (`created_by`),
UNIQUE KEY `modified_by` (`modified_by`)
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) unsigned NOT NULL auto_increment,
`username` varchar(25) NOT NULL,
`password` varchar(255) NOT NULL,
`usertype_id` int(11) unsigned NOT NULL,
`active` tinyint(1) NOT NULL,
`created` datetime NOT NULL,
`modified` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `usertype_id` (`usertype_id`)
)
--
-- Constraints for table `categories`
--
ALTER TABLE `categories`
ADD CONSTRAINT `categories_ibfk_1` FOREIGN KEY (`created_by`) REFERENCES `users` (`id`) ON DELETE NO ACTION ON UPDATE CASCADE,
ADD CONSTRAINT `categories_ibfk_2` FOREIGN KEY (`modified_by`) REFERENCES `users` (`id`) ON DELETE NO ACTION ON UPDATE CASCADE;
Hi have a table named category which has two foreign keys to the table user, structure is given below. How can I get the user name corresponding to created_by and modified_by by using relations. @category = Category.find(params[:id])
is giving only the details from the category table. My current model class is
class Category < ActiveRecord::Base
validates_uniqueness_of :title, :message => "Title already exist"
validates_presence_of :title, :message => "Cannot be blank"
belongs_to :user
end
How can I associate both the fields created_by and modified_by to the user model
CREATE TABLE IF NOT EXISTS `categories` (
`id` int(11) unsigned NOT NULL auto_increment,
`title` varchar(255) NOT NULL,
`created_by` int(11) unsigned default NULL,
`modified_by` int(11) unsigned default NULL,
`created` datetime NOT NULL,
`modified` timestamp NOT NULL default CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `created_by` (`created_by`),
UNIQUE KEY `modified_by` (`modified_by`)
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) unsigned NOT NULL auto_increment,
`username` varchar(25) NOT NULL,
`password` varchar(255) NOT NULL,
`usertype_id` int(11) unsigned NOT NULL,
`active` tinyint(1) NOT NULL,
`created` datetime NOT NULL,
`modified` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `usertype_id` (`usertype_id`)
)
--
-- Constraints for table `categories`
--
ALTER TABLE `categories`
ADD CONSTRAINT `categories_ibfk_1` FOREIGN KEY (`created_by`) REFERENCES `users` (`id`) ON DELETE NO ACTION ON UPDATE CASCADE,
ADD CONSTRAINT `categories_ibfk_2` FOREIGN KEY (`modified_by`) REFERENCES `users` (`id`) ON DELETE NO ACTION ON UPDATE CASCADE;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该适合你:
This should work for you: