Dave Wong
Dave Wong

Reputation: 335

ROR : add_index not working?

I'm new to ROR, what i trying to do is to create two table and link together, but i can't make the relation correctly, can anybody help?

i had 2 Models, Model "User" and Model "Release", i want to say a user has_many release,linked by user_id

rails generate scaffold user name email ...
rails generate scaffold release title text:description ...

craete_user.rb

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :name
      t.string :email
      t.string :password
      t.string :releaseCount
      t.timestamps
    end
  end
end

create_release.rb

 class CreateReleases < ActiveRecord::Migration
  def change
    create_table :releases do |t|
      t.string :title
      t.text :desc
      t.integer :user_id
      t.timestamps
    end
    add_index :releases, :user_id
  end
end

release.rb

class Release < ActiveRecord::Base
    belongs_to :user
end

user.rb

class User < ActiveRecord::Base
    has_many :release
end

At this point, i can create user and release , but i cant link them together.Finally, i got this result in console

  u = User.find(1)
  User Load (0.4ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
 => #<User id: 1, name: "dave", email: "[email protected]", password: "dave", releaseCount: "0", created_at: "2014-05-11 03:42:16", updated_at: "2014-05-11 03:42:16"> 
2.1.1 :024 > u.create_release
NoMethodError: undefined method `create_release' for #<User:0x000001029c71d0>
    from /Users/daywong1119/.rvm/gems/ruby-2.1.1/gems/activemodel-4.1.0/lib/active_model/attribute_methods.rb:435:in `method_missing'
    from /Users/daywong1119/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/attribute_methods.rb:206:in `method_missing'
    from (irb):24

Upvotes: 1

Views: 233

Answers (3)

Chase
Chase

Reputation: 2826

Per the answer below, pluralize

has_many :resources

You will also need to add a user in the console.

u = User.find(1)
r = release.create(title: "title", desc: "some description", user_id: u.id)

or you can shovel a user into the release

u = User.find(1)
r = release.new(title: "title", desc: "some description")
r << u
r.save

The << operator is called a shovel, check it out in the ruby docs.

In your view you'll need to add user_id as a hidden field in the form.

Upvotes: 0

nathanvda
nathanvda

Reputation: 50057

You call the incorrect method. The create_<association_name> only works for an association with a single related item, e.g. a belongs_to or has_one.

So your assocations should be defined as follows:

class User
  has_many :releases
end

class Release
  belongs_to :user
end

And then you can write

u = User.find(1)
u.releases.create(...)

alternatively, you can also write

r = Release.create(....)
user = r.create_user

Upvotes: 0

SNEH PANDYA
SNEH PANDYA

Reputation: 797

update your user model:

class User < ActiveRecord::Base
  has_many :release
end

to

class User < ActiveRecord::Base
  has_many :releases
end

has_many should be in pural.

Upvotes: 2

Related Questions