我有一个membership
模型,它具有以下关联:
class Membership < ActiveRecord::Base
belongs_to :family_tree
belongs_to :member
belongs_to :inviter, class_name: "User", foreign_key: "user_id"
belongs_to :invited, class_name: "User", foreign_key: "invited_id"
end
这是架构:
# == Schema Information
#
# Table name: memberships
#
# id :integer not null, primary key
# family_tree_id :integer
# user_id :integer
# relation :string(255)
# member_id :integer
# invited_id :integer
#
在我的User
型号上,我有以下内容:
class User < ActiveRecord::Base
has_many :memberships
end
所以我可以成功地将2个不同的用户分配给1个记录,例如:
[7] pry(#<RSpec::ExampleGroups::User>)> membership
=> #<Membership id: 90, family_tree_id: 1049, user_id: 779, created_at: "2015-10-29 15:24:02", updated_at: "2015-10-29 15:24:02", relation: "sister", member_id: 108, connection_sent_at: nil, connection_responded_at: nil, connect_send_limit: nil, connect_times_sent: nil, connected: nil, connect_type: nil, request_status: nil, connection_removed_at: nil, invited_id: 777>
[8] pry(#<RSpec::ExampleGroups::User>)> membership.family_tree
=> #<FamilyTree id: nil, name: "Enrico McGlynn", user_id: 777, created_at: "2015-10-29 15:24:02", updated_at: "2015-10-29 15:24:02">
[11] pry(#<RSpec::ExampleGroups::User>)> membership.inviter
=> #<User id: nil, email: "odell@lowe.net", encrypted_password: "$2a$04$Ke.1sQ9mZ2ZP0Hxamg6NS.6zUoEHyolPbKJ1UfAlils...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2015-10-29 15:24:02", updated_at: "2015-10-29 15:24:02", first_name: "Audie", confirmation_token: nil, confirmed_at: "2015-10-29 15:23:57", confirmation_sent_at: nil, unconfirmed_email: nil, invitation_relation: "quibusdam", avatar: nil, invitation_token: nil, invitation_created_at: nil, invitation_sent_at: nil, invitation_accepted_at: nil, invitation_limit: nil, invited_by_id: nil, invited_by_type: nil, invitations_count: 0, bio: "Quibusdam est laborum cupiditate modi et est. Nece...", last_name: "Feil", gender: 1>
[12] pry(#<RSpec::ExampleGroups::User>)> membership.invited
=> #<User id: nil, email: "jalon_greenfelder@hudson.name", encrypted_password: "$2a$04$jlFrLS6UlcchaJlsOAv4ne/5nYSxH.7Zdsvk5WZMd1w...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2015-10-29 15:24:02", updated_at: "2015-10-29 15:24:02", first_name: "Annamae", confirmation_token: nil, confirmed_at: "2015-10-29 15:23:57", confirmation_sent_at: nil, unconfirmed_email: nil, invitation_relation: "eveniet", avatar: nil, invitation_token: nil, invitation_created_at: nil, invitation_sent_at: nil, invitation_accepted_at: nil, invitation_limit: nil, invited_by_id: nil, invited_by_type: nil, invitations_count: 0, bio: "Ut explicabo omnis facilis et. Dolor sunt enim et....", last_name: "Kshlerin", gender: 1>
我遇到的问题是当我试图走另一条路时,即从该用户记录中找到与之关联的所有memberships
。它返回一个空数组。
[1] pry(#<RSpec::ExampleGroups::User>)> u1
=> #<User id: nil, email: "jalon_greenfelder@hudson.name", encrypted_password: "$2a$04$jlFrLS6UlcchaJlsOAv4ne/5nYSxH.7Zdsvk5WZMd1w...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2015-10-29 15:24:02", updated_at: "2015-10-29 15:24:02", first_name: "Annamae", confirmation_token: nil, confirmed_at: "2015-10-29 15:23:57", confirmation_sent_at: nil, unconfirmed_email: nil, invitation_relation: "eveniet", avatar: nil, invitation_token: nil, invitation_created_at: nil, invitation_sent_at: nil, invitation_accepted_at: nil, invitation_limit: nil, invited_by_id: nil, invited_by_type: nil, invitations_count: 0, bio: "Ut explicabo omnis facilis et. Dolor sunt enim et....", last_name: "Kshlerin", gender: 1>
[9] pry(#<RSpec::ExampleGroups::User>)> u1.memberships
=> []
如何让user.memberships
在user_id and invited_id
模型上返回与memberships
相关联的成员资格?
答案 0 :(得分:0)
Membership.where(user_id: user.id)
相当于memberships
。您应该注意User
中与user_id
模型关联的foreign_key默认为class User < ActiveRecord::Base
#user.memberships will be equivalent to Membership.where(user_id: user.id)
has_many :memberships
#user.invited_memberships will be equivalent to Membership.where(inviter_id: user.id)
has_many :invited_memberships, class_name: Membership, foreign_key: :inviter_id
def all_memberships
#this will return the memberships associated with the user_id and the inviter_id
#you may want to move this to the Membership model
Membership.where('user_id = ? OR inviter_id = ?', self.id, self.id)
end
end
。要解决您的问题:
m <- regexpr(paste0(x,collapse=""),paste0(z,collapse = ""),fixed=T)
seq(m,length.out=length(x))