Osborne Saka
Osborne Saka

Reputation: 509

NoMethodError (undefined method `route=' for #<ActiveRecord:0x00007febf2e806f8>

I need some help here. I have a community model with belongs to an account. I use devise for the authentication stuff. Now the issue is when I try to submit/create a community I get this error 'undefined method `account_id=' for #Community:0x00007febf2e806f8 Did you mean? account='

controller

    def create
        @community = Community.new comunity_values
        @community.account_id = current_account.id

        if @community.save
            redirect_to community_path
        else 
            render :new
        end
    end
    
    private
    def comunity_values
        params.require(:community).permit(:name, :url,:rules)
    end
end

model

class Community <ApplicationRecord
    belongs_to :account
    validates_presence_of :url, :name , :rules
end

migrations

class CreateCommunities < ActiveRecord::Migration[6.0]
  def change
    create_table :communities do |t|
      t.references :account
      t.string :name
      t.string :url
      t.text :rules
      t.string :total_members
      t.timestamps
      
    end
  end
end

Upvotes: 1

Views: 305

Answers (1)

In model account you have to put the reference to communities model too. Something like:

has_many :community

Other think you have to know, is that you don't need to specify the id when you create using relations, you could use in a more readable way:

@community.account = current_account

@community.save

And I think that you are inverting the order of things, because one account has many communities so... You could do that as follows:

current_account.community.create!(comunity_values)

Hope this helps

Upvotes: 1

Related Questions