Rails has_many和has_many通过(Rails has_many and has_many through)
我很困惑如何接近这个。 我通过会员模型连接用户和组,但我也希望用户能够创建新组。 显然,组必须属于用户,但组也通过成员资格表属于用户。
我在我的user.rb文件中有这个,但我觉得这是错的。 我是否删除了第一个而只是通过一个? 在这种情况下,我如何在小组的创建者中工作?
class User < ApplicationRecord has_many :groups has_many :groups, through: :memberships end
换句话说,用户是许多组的成员,但也是许多组的创建者。 成员资格表只有两列(组ID和用户ID)。 此列中的用户标识用于存储属于该组成员的用户。 我对如何处理创建该组的用户感到困惑。
I am confused on how to go about approaching this. I am connecting users and groups through the membership model, but I also want users to be able to create new groups. Clearly a group must then belong to a user, but the groups also belong to users through the memberships table.
I have this in my user.rb file, but I feel it is wrong. Do I remove the first one and just have the through one? How do I work in the creator of the group in that case?
class User < ApplicationRecord has_many :groups has_many :groups, through: :memberships end
In other words, the user is a member of many groups, but also the creator of many groups. The memberships table only has two columns (group id and user id). The user id in this column is used to store users who are members of that group. I am stuck on what to do about the user who created the group.
原文:https://stackoverflow.com/questions/44270507
最满意答案
h2ooooooo弄清楚了。 我取消注释了错误的部分,将其设置为On。 它后来在页面上回归。 一切都很好。
h2ooooooo got it figured out. I uncommented the wrong section to set it to On. It was turing back off later down the page. All good now.
相关问答
更多-
如果时区在相关的php.ini文件中设置正确,并且您仍然收到此消息,则可以尝试设置TZ环境变量。 编辑.profile以添加以下行(在您自己的时区字符串中 ): export TZ="America/New_York" 不知道为什么(a)MAMP会覆盖您的php.ini设置,(b)即使使用TZ env变量,PHP也不会在使用TZ env变量时发出通知/警告,但此解决方案适用于使用MAMP 2.0.5与PHP 5.3.6。 If the timezone is set correctly in the pe ...
-
可能不是你想要的长期,但可能是一个临时解决方案。 如果使用@为PHP函数调用加前缀,则它们不会输出警告。 例: 5); ec ...
-
phpinfo字段的来源(Source of phpinfo fields)[2022-12-08]
如果通过ini_set,.htaccess php_value或php.ini设置了一个选项,则无法区分。 There is no way to differentiate if an option was set via ini_set, .htaccess php_value or php.ini. -
PHP ini_set php.net手册中的display_errors示例说明(PHP ini_set php.net manual display_errors example explanation)[2024-01-26]
我们为什么在将其设置为true之前检查display_errors是否为false有充分的理由吗? 这是一个例子。 不,没有更深的原因。 用“真”覆盖“真”也是有效的。 毫无疑问,如果这是我们的意图,那么确实会更快一些,并将其设置为真? 我想你很少会觉得有什么不同,不管你先检查一下,不管。 但是:您应该使用php.ini来实现这一点,并且更重要的是: 切勿在实时系统上启用display_errors ! Is there a good reason as to why we're checking to s ... -
我的坏...意识到我在测试页面上没有使用的其中一个是display_errors被动态设置为1的原因。 对Shrapnel上校表示持怀疑态度(赞成他的评论) My bad.. realized that one of my includes which I wasn't using on test pages was the cause of display_errors being set to 1 dynamically. Col. Shrapnel was write to be skeptical ...
-
Php display_errors(Php display_errors)[2023-05-20]
通过使用以下代码并在php.ini文件中显示display_errors = on error_reporting(E_ALL); ini_set('display_errors',1); ini_set('error_reporting', E_ALL); ini_set('display_startup_errors',1); error_reporting(-1); do this by using the following code and inside you php.ini file dis ... -
display_errors设置为On但在phpinfo()中显示Off(display_errors set to On but shows Off in phpinfo())[2023-05-05]
h2ooooooo弄清楚了。 我取消注释了错误的部分,将其设置为On。 它后来在页面上回归。 一切都很好。 h2ooooooo got it figured out. I uncommented the wrong section to set it to On. It was turing back off later down the page. All good now. -
这与iMagick的PHP模块的安装方式有关。 我建议你重新访问你的安装并正确安装。 然后重新访问PHPINFO以确认您看到所有必要的格式。 在Debian上安装 http://rakesh.sankar-b.com/2011/05/25/install-imagemagick-from-source-in-debian/ 在CentOS上安装 http://rakesh.sankar-b.com/2010/07/13/how-to-install-imagemagick-linux-yum/ http:/ ...
-
使用Apache中的PHP和命令行是两回事: 你把libmysql放到apach bin-folder中。 当您从命令行启动php时,必须也可以从那里访问libmysql,例如。 当它在PATH中。 Tried many suggestions but the one that worked was remove everything and start over. Got rid of WAMP and loaded XAMPP now mySQL is up and running using PDO. ...
-
该错误来自您的die命令。 您无法通过关闭错误来抑制die ,因为您在退出之前明确地将mysql_error回显到屏幕。 您应该以与上面的mysql_select_db行相同的方式使用trigger_error来解决此问题: mysql_query("SELECT * FROM `FakeTable`") OR trigger_error(mysql_error(), E_USER_ERROR); That error is from your die command. You cannot suppre ...