在rails中加入table;多对多;尝试在实例上调用 join 方法
Join table in rails; many-to-many; trying to call join method on instance
我几个小时前刚刚做了这个,现在我无法让它在第二次尝试中工作。有很多用户和很多事件目标。 Users have many EventGoals and EventGoals have many Users,两者都通过加入table称为EventGoalClaims。 @event_goal 正确通过,但我得到一个 `
undefined method `event_goal_claims' for #<EventGoal:0x007fd11b0ce178>`
视图如下(第一行出错):
<%= form_for([@event_goal, @event_goal.event_goal_claims.build]) do |f| %>
<%= f.hidden_field :user_id, :value => current_user.id %>
<%= f.hidden_field :user_last_name, :value => current_user.last_name %>
控制器
def create
@event_goal = EventGoal.find(params[:event_goal_id])
@event_goal_claim = @event_goal.event_goal_claims.build(eventgoalclaim_params)
@event_goal_claim.event_goal_id = @event_goal.id
@event_goal_claim.user_id = current_user.id
@event_goal_claim.user_last_name = current_user.last_name
...
private
def eventgoalclaim_params
params.require(:event_goal_claim).permit(:event_goal_id, :user_id, :user_last_name, :approved)
end
编辑
class EventGoal < ActiveRecord::Base
belongs_to :event
has_many :users, through: :event_goal_claims
end
答案可能在于三个模型之一的关联。确保它们看起来像这样:
user.rb
Class User < ActiveRecord::Base
has_many :event_goal_claims
has_many :event_goals, through: :event_goal_claims
event_goal.rb
Class EventGoal < ActiveRecord::Base
has_many :event_goal_claims
has_many :users, through: :event_goal_claims
event_goal_claim.rb
Class EventGoalClaim < ActiveRecord::Base
belongs_to :user
belongs_to :event_goal
还值得注意:这都是假设您的数据库在 schema.rb
中的 event_goal_claims table
上有外键标记为 user_id and event_goal_id
。如果它们与此不同,您将需要在模型的关联中明确说明外键的名称。
我几个小时前刚刚做了这个,现在我无法让它在第二次尝试中工作。有很多用户和很多事件目标。 Users have many EventGoals and EventGoals have many Users,两者都通过加入table称为EventGoalClaims。 @event_goal 正确通过,但我得到一个 `
undefined method `event_goal_claims' for #<EventGoal:0x007fd11b0ce178>`
视图如下(第一行出错):
<%= form_for([@event_goal, @event_goal.event_goal_claims.build]) do |f| %>
<%= f.hidden_field :user_id, :value => current_user.id %>
<%= f.hidden_field :user_last_name, :value => current_user.last_name %>
控制器
def create
@event_goal = EventGoal.find(params[:event_goal_id])
@event_goal_claim = @event_goal.event_goal_claims.build(eventgoalclaim_params)
@event_goal_claim.event_goal_id = @event_goal.id
@event_goal_claim.user_id = current_user.id
@event_goal_claim.user_last_name = current_user.last_name
...
private
def eventgoalclaim_params
params.require(:event_goal_claim).permit(:event_goal_id, :user_id, :user_last_name, :approved)
end
编辑
class EventGoal < ActiveRecord::Base
belongs_to :event
has_many :users, through: :event_goal_claims
end
答案可能在于三个模型之一的关联。确保它们看起来像这样:
user.rb
Class User < ActiveRecord::Base
has_many :event_goal_claims
has_many :event_goals, through: :event_goal_claims
event_goal.rb
Class EventGoal < ActiveRecord::Base
has_many :event_goal_claims
has_many :users, through: :event_goal_claims
event_goal_claim.rb
Class EventGoalClaim < ActiveRecord::Base
belongs_to :user
belongs_to :event_goal
还值得注意:这都是假设您的数据库在 schema.rb
中的 event_goal_claims table
上有外键标记为 user_id and event_goal_id
。如果它们与此不同,您将需要在模型的关联中明确说明外键的名称。