0

My code works when I run the action like this,

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

But,why do I get an undefined method for 'interest' when I use this line in my Interest action?

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

Here's my Idea model

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

Here's my User model (Devise)

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

Here is the button_to tag

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

And my route,

resources :ideas do
    resources :interests
end

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

has_many :users

:idea_id :user_id

end

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

activerecord (4.2.0)

8
  • Add your interest model and error too. Commented Mar 14, 2015 at 19:11
  • Add has_and_belongs_to_many :interests in your Idea model. Commented Mar 14, 2015 at 19:19
  • where is your Interest model? Where did you add? Commented Mar 14, 2015 at 19:23
  • @SharvyAhmed Tried it from idea and interest and the relationship did not change the error. Commented Mar 14, 2015 at 19:23
  • @SharvyAhmed check again, I think an edit, deleted it prior. Commented Mar 14, 2015 at 19:25

1 Answer 1

1

I think you messed up the association, I'd do:

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

Then I guess the rest would work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.