我有两个模型client
和user
。
client
可以包含多个administrators
,而user
可以是administrator
的{{1}}。
我发现couple people建议使用clients
建议使用has_many :through
建立这种关系的更好方法,而不是has_and_belongs_to_many
。
class V1::User < ActiveRecord::Base
has_many :administrators,
class_name: 'V1::ClientAdministrator'
has_many :clients,
through: :administrators
class_name: 'V1::Clients'
class V1::Client < ActiveRecord::Base
has_many :users,
class_name: "V1::User"
has_many :administrators,
through: :users,
class_name: "V1::ClientAdministrator"
validates :administrators,
length: { minimum: 1}
class V1::ClientAdministrator < ActiveRecord::Base
belongs_to :v1_user
belongs_to :v1_client
end
rails c
u = V1::User.create!(name: 'test_user')
c = V1::Client.new(name: 'test_client')
c.administrators << u
ActiveRecord::AssociationTypeMismatch: V1::ClientAdministrator(#70347494104080) expected, got V1::User(#70347494299440)
在切换到has_many :through
之前,我已成功使用has_and_belongs_to_many
:
class V1::Client < ActiveRecord::Base
has_and_belongs_to_many :administrators,
-> { uniq },
join_table: :v1_client_administrators,
foreign_key: "v1_client_id",
association_foreign_key: "v1_user_id",
class_name: "V1::User"
这种方法的问题是我无法对关联进行任何验证,例如before_destroy确保仍然有一个管理员。此外,我可能会在将来为该关系添加元数据。
administrators
的{{1}}?client
是否有任何方式可以成为一组用户,而不是强迫我client.administrators
来访问用户? (如果我最终添加元数据,这可能是没有意义的)client.administrators.each { |admin| admin.user}
支持client.users
?client.administrators
的正确方法吗?答案 0 :(得分:0)
我相信这是你正在寻找的,请恰当地命名:
- (void)webViewDidFinishLoad:(UIWebView *)webView2
{
NSLog(@"webViewDidFinishLoad ...");
NSString *htmlSourceCodeStr = [webView2 stringByEvaluatingJavaScriptFromString:@"document.documentElement.outerHTML"];
}