Basic Relationship Problem

I’m trying to do a basic relationship and it’s not working the way it
should. The data model is obviously a test database as the fields are
minimal.

In irb I do the following:

user = User.create(:name => “shawn”)
=> #<User:0x259ceac @errors=#<ActiveRecord::Errors:0x259c3a8
@errors={}, @base=#<User:0x259ceac …>>, @attributes={“name”=>“shawn”,
“id”=>2}, @new_record=false>

user.profile.create(:hello => “yeah”)
NoMethodError: undefined method profile' for #<User:0x259ceac> from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1792:inmethod_missing’
from (irb):14
from :0

I don’t understand why it doesn’t understand the relationship.
Following is my ruby code:

class User < ActiveRecord::Base

has_one :profile

end

class Profile < ActiveRecord::Base

belongs_to :user

end

ActiveRecord::Schema.define(:version => 3) do

create_table “profiles”, :force => true do |t|
t.column “hello”, :string
t.column “user_id”, :integer
end

create_table “users”, :force => true do |t|
t.column “name”, :string
end

end

I know I’m missing something basic to make this work. It’s driving me
up the wall. Any help would be appreciated.

Instead of …

user.profile.create(:hello => "yeah")

try this…

user.profiles << Profile.new(:hello => "yeah")

(as in, adding a new member to the user.profiles collection, that new
member being a new instance of class Profile)

c.

Shawn S. wrote:

I’m trying to do a basic relationship and it’s not working the way it
should. The data model is obviously a test database as the fields are
minimal.

In irb I do the following:

user = User.create(:name => “shawn”)
=> #<User:0x259ceac @errors=#<ActiveRecord::Errors:0x259c3a8
@errors={}, @base=#<User:0x259ceac …>>, @attributes={“name”=>“shawn”,
“id”=>2}, @new_record=false>

user.profile.create(:hello => “yeah”)
NoMethodError: undefined method profile' for #<User:0x259ceac> from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1792:inmethod_missing’
from (irb):14
from :0

I don’t understand why it doesn’t understand the relationship.
Following is my ruby code:

class User < ActiveRecord::Base

has_one :profile

end

class Profile < ActiveRecord::Base

belongs_to :user

end

ActiveRecord::Schema.define(:version => 3) do

create_table “profiles”, :force => true do |t|
t.column “hello”, :string
t.column “user_id”, :integer
end

create_table “users”, :force => true do |t|
t.column “name”, :string
end

end

I know I’m missing something basic to make this work. It’s driving me
up the wall. Any help would be appreciated.

No, that gives the same errors as before:

user
=> #<User:0x259ceac @errors=#<ActiveRecord::Errors:0x259c3a8 @errors={},
@base=#<User:0x259ceac …>>, @attributes={“name”=>“shawn”, “id”=>2},
@new_record=false>

user.profiles << Profile.new(:hello => “yeah”)
NoMethodError: undefined method profiles' for #<User:0x259ceac> from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1792:inmethod_missing’
from (irb):18
from :0

I also tried it with user.profile << and using create instead of new.
Does the foreign key need to be defined in MySQL or does RoR understand
the relationship through its belongs_to and has_one methods?

Cayce B. wrote:

Instead of …

user.profile.create(:hello => "yeah")

try this…

user.profiles << Profile.new(:hello => "yeah")

(as in, adding a new member to the user.profiles collection, that new
member being a new instance of class Profile)

c.

Shawn S. wrote:

I’m trying to do a basic relationship and it’s not working the way it
should. The data model is obviously a test database as the fields are
minimal.

In irb I do the following:

user = User.create(:name => “shawn”)
=> #<User:0x259ceac @errors=#<ActiveRecord::Errors:0x259c3a8
@errors={}, @base=#<User:0x259ceac …>>, @attributes={“name”=>“shawn”,
“id”=>2}, @new_record=false>

user.profile.create(:hello => “yeah”)
NoMethodError: undefined method profile' for #<User:0x259ceac> from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1792:inmethod_missing’
from (irb):14
from :0

I don’t understand why it doesn’t understand the relationship.
Following is my ruby code:

class User < ActiveRecord::Base

has_one :profile

end

class Profile < ActiveRecord::Base

belongs_to :user

end

ActiveRecord::Schema.define(:version => 3) do

create_table “profiles”, :force => true do |t|
t.column “hello”, :string
t.column “user_id”, :integer
end

create_table “users”, :force => true do |t|
t.column “name”, :string
end

end

I know I’m missing something basic to make this work. It’s driving me
up the wall. Any help would be appreciated.

My mistake - I was thinking “has_many”, didn’t connect that it was
“has_one”.

How about:

user.profile = Profile.new(:hello => "yeah")

c.

Shawn Szauksztun-Zvinis wrote:

No, that gives the same errors as before:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs