Ruby on rails Rails与数组的关联条件
我对这个协会有意见 之前:Ruby on rails Rails与数组的关联条件,ruby-on-rails,Ruby On Rails,我对这个协会有意见 之前: has_many :membership, class_name: 'Profile', conditions: "profiles.role != 'owner'" 当前属性“role”不再是字符串,现在是数组,因此我需要更改该条件,以生成“role”为['owner'](而不是'owner')的记录,但我无法将数组与匹配项关联起来 通缉: has_many :memberships, class_name: 'Profile', conditions: "p
has_many :membership, class_name: 'Profile', conditions: "profiles.role != 'owner'"
当前属性“role”不再是字符串,现在是数组,因此我需要更改该条件,以生成“role”为['owner'](而不是'owner')的记录,但我无法将数组与匹配项关联起来
通缉:
has_many :memberships, class_name: 'Profile', conditions: "profiles.role != ['owner']"
剖面模型
class Profile < ActiveRecord::Base
attr_accessible :role
serialize :role, Array
belongs_to :account
def roles?(role)
role.include?(role)
end
end
类配置文件
角色列在数据库中的外观如何?它是一根绳子吗
如果它是一个varchar列,那么下面的代码应该可以工作
has_many :memberships, class_name: 'Profile', conditions: "profiles.role not in ('owner')"
我找到了解决办法。正确的查询是:
`profiles`.`role` NOT LIKE '%owner%'
使用arel:
Profile.arel_table[:role].does_not_match('%owner%').to_sql
结果:
profiles`.`role` NOT LIKE '%owner%
我猜即使列类型“string”被序列化,它仍然是sql用于搜索的“string”。但是,这也是一个假设。您能在配置文件模型中提供角色定义吗?您的建议无效。。我不知道为什么,我以前试过,现在又试过,但它仍然返回所有配置文件。所有者不能是其他任何东西,因此这就是所有者配置文件的外观。
常规配置文件的外观。
角色列是字符串类型,我在我的配置文件模型上序列化了它。序列化:角色,数组
您可以粘贴“从配置文件限制1中选择*的结果”吗?