Reputation: 160
I am stuck at a small problem,but it has been so long for me trying to figure out what am i doing wrong. My scenario is i have a existing model user
and now i create another model called `user_comment'. I have created below mentioned details:
User model:
class User < ActiveRecord::Base
has_many :user_comments
end
User_comment Model:
class UserComment < ActiveRecord::Base
belongs_to :user
end
Migration File:
class CreateUserComments < ActiveRecord::Migration
def change
create_table :user_comments do |t|
t.integer :user_id
t.string :comments
t.timestamps
end
end
end
After running rake db:migrate
i went to rails console
and then to setup the relation between two tables i did the following and nothing is working
obj1= User.first
I added first new row in user_comments table and then did ..
obj2= UserComment.first
Doing obj1.obj2= obj2
is giving me
NoMethodError: undefined method `obj2=' for #<User:0x00000005f8e850>
from /home/insane/.rvm/gems/ruby-2.1.0/gems/activemodel-3.2.11/lib/active_model/attribute_methods.rb:407:in `method_missing'
from /home/insane/.rvm/gems/ruby-2.1.0/gems/activerecord-3.2.11/lib/active_record/attribute_methods.rb:149:in `method_missing'
from (irb):3
from /home/insane/.rvm/gems/ruby-2.1.0/gems/railties-3.2.11/lib/rails/commands/console.rb:47:in `start'
from /home/insane/.rvm/gems/ruby-2.1.0/gems/railties-3.2.11/lib/rails/commands/console.rb:8:in `start'
from /home/insane/.rvm/gems/ruby-2.1.0/gems/railties-3.2.11/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
Please help me how to form an association ..
Upvotes: 0
Views: 295
Reputation: 76784
obj1.obj2= obj2
=> self-referential loop?
You'll be best reading the Rails ActiveRecord associations guide - you'll find what you're dealing with is actually relatively simple to remedy
As described by @rails4guides.com
, you'll be best renaming your UserComment
model to just Comment
(as this will allow you to associate any data you need with it -- if you wanted to extend later):
#app/models/user.rb
Class User < ActiveRecord::Base
has_many :comments
end
#app/models/comment.rb
Class Comment < ActiveRecord::Base
belongs_to :user
end
This is actually down to relational database naming conventions, whereby each table's data can be associated with another's. They do this with foreign_keys
- which is how Rails uses the associations defined in your models to create the likes of @user.comments
:
class Comments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.integer :user_id => this is the foreign_key, which means Rails can append these objects to your `user` object
t.string :comments
t.timestamps
end
end
end
So if you want to give the user a set of comments, you'll just need to call the User
object from your model, and since Rails' ActiveRecord associations automatically call any relational data through your schemas, you'll get comments
appended to the @user
object with @user.comments
Upvotes: 1
Reputation: 1441
Where does SmileComment suddenly come from?
Anyway, there are several things going wrong. First off, I would change the name of your model from UserComment to Comment. The fact that a comment belongs to an user is already made clear through your association. Calling User.first.user_comments seems a bit akward.
Let us start with a really basic example:
class User < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :user
end
As you did in your migration, the comments needs a user_id to reference the user it belongs to. After running the migration, calling the association is dead simple:
User.first.comments # Gives all comments belonging to that user
Or:
Comment.first.user # Gives the user that belongs to that comment
Upvotes: 1
Reputation: 160933
obj1.obj2= obj2
is wrong, you need a space between obj1.obj2
and =
.
But ob1.obj2
make no sense too(there is no obj2
method in User
).
Add an object to an association you could do:
user = User.first
comment = UserComment.first
# if both object are not nil, then you could do below
user.user_commentes << comment
Upvotes: 2