一个用户拥有多个职位。这是 Auth 控制器的一部分,它调用 LinkedIn 以供用户进行身份验证:
positions.each do |position|
@li_pos_id = position.id
@title = position.title
@company = position.company.name
@industry = position.company.industry
@start_month = position.start_date.month
@start_year = position.start_date.year
@end_month = position.end_date.month
@end_year = position.end_date.year
current_user.positions.build(li_pos_id: @li_pos_id, title: @title, company: @company, industry: @industry,
start_month: @start_month, start_year: @start_year, end_month: @end_month, end_year: @end_year)
end
我正在努力弄清楚为什么 current_user.positions.build(...) 在我的开发(Postgres)数据库中而不是在 Heroku(也是 Postgres)中创建一个位置。我的用户对象在身份验证后创建得很好。从我的会话控制器:
def create
user = User.from_omniauth(env['omniauth.auth'])
session[:user_id] = user.id
redirect_to auth_path
end
从我的用户模型中调用以下内容:
def self.from_omniauth(auth)
where(auth.slice('provider', 'uid')).first || create_from_omniauth(auth)
end
def self.create_from_omniauth(auth)
create! do |user|
user.provider = auth['provider']
user.uid = auth['uid']
end
end
我的用户模型包含必要的“has_many :positions”,而我的职位模型包含“belongs_to :user”。无论如何, current_user.positions.build(...) 行在开发中创建了适当的位置,但在生产中根本没有。
非常感谢这里的任何帮助!谢谢你。