RoR——模型关系

发布于 2024-10-04 04:53:25 字数 1599 浏览 1 评论 0原文

您好,有一个名为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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨落画卷 2024-10-11 04:53:25

这应该适合你:

class Category < ActiveRecord::Base
  # ...
  validates_uniqueness_of :title, :message => "Title already exist"
  validates_presence_of :title, :message => "Cannot be blank"
  belongs_to :creator, :foreign_key => 'created_by', :class_name => 'User'
  belongs_to :editor, :foreign_key => 'modified_by', :class_name => 'User' 
  # ...
end

class User < ActiveRecord::Base
  # ...
  has_many :created_categories, :class_name => 'Category', :foreign_key => 'created_by'                                                                                
  has_many :modified_categories, :class_name => 'Category', :foreign_key => 'modified_by'
  # ...
end

This should work for you:

class Category < ActiveRecord::Base
  # ...
  validates_uniqueness_of :title, :message => "Title already exist"
  validates_presence_of :title, :message => "Cannot be blank"
  belongs_to :creator, :foreign_key => 'created_by', :class_name => 'User'
  belongs_to :editor, :foreign_key => 'modified_by', :class_name => 'User' 
  # ...
end

class User < ActiveRecord::Base
  # ...
  has_many :created_categories, :class_name => 'Category', :foreign_key => 'created_by'                                                                                
  has_many :modified_categories, :class_name => 'Category', :foreign_key => 'modified_by'
  # ...
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文