2015-03-14 2 views
0

Мой код работает, когда я запускаю действие, как это,Rails не может создать запись с Active Record Ассоциации

def interest 
    @interest = Interest.new 
    @interest.user_id = current_user.id 
    @interest.idea_id = @idea.id 
    @interest.save 
    @ideas = Idea.take(10) 
    flash[:notice] = "A message has been sent to the poster." 
    render "ideas/forum" 
end 

Но, почему я получаю неопределенный метод «интерес», когда я использую эту линию в моих интересах действие?

@interest = current_user.ideas.interest.create(params[:interest]) 

Вот моя идея модель

class Idea < ActiveRecord::Base 
    belongs_to :user 
    :title 
    :category 
    :content 
    :user_id 
    :createdDate 
    :updatedDate 

Вот моя модель пользователя (Завещание)

class User < ActiveRecord::Base 
    has_many :ideas 
    has_many :interests 
end 

здесь является button_to тег

<%= button_to "I'm Interested", ideas_interest_path(:id => idea.id, :idea_id => idea.id, :user_id => idea.user_id) ,class: 'btn btn-primary' %> 

И мой маршрут,

resources :ideas do 
    resources :interests 
end 

Интерес Модель класс Процентный < ActiveRecord :: Base belongs_to: Пользователь belongs_to: идея

has_many: пользователи

: idea_id : user_id

конец

NoMethodError - undefined method `interest' for #<Idea::ActiveRecord_Associations_CollectionProxy:0x007f9e5189bab0>: 

ActiveRecord (4.2.0)

+0

Добавьте свою модель интереса и ошибку. –

+0

@SharvyAhmed добавлен. – snowYetis

+0

Добавьте 'has_and_belongs_to_many: интересы' в своей модели Idea. –

ответ

1

Я думаю, что вы испортили ассоциацию, я бы:

class User < ActiveRecord::Base 
    has_many :interests 
    has_many :ideas, through: :interests 
end 

class Interest < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :idea 

    # user_id, idea_id 
end 

class Idea < ActiveRecord::Base 
    has_many :interests 
    has_many :users, through: :interests 
end 

Тогда я думаю, остальное будет работать.