Hello guys, I have two tables, tasks and projects, and each model I put: Task.rb class Task < ActiveRecord::Base attr_accessible :user_id, :project_id, :name belongs_to :project end Project.rb class Project < ActiveRecord::Base attr_accessible :name, :description has_many :tasks end But when I go to my prompt and I make a SELECT with task, none project is returned. irb(main):001:0> Task.all ←[1m←[36mTask Load (0.0ms)←[0m ←[1mSELECT `tasks`.* FROM `tasks` ←[0m ←[1m←[35mEXPLAIN (30.0ms)←[0m EXPLAIN SELECT `tasks`.* FROM `tasks` EXPLAIN for: SELECT `tasks`.* FROM `tasks` +----+-------------+-------+------+---------------+------+---------+------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+------+---------+------+------+-------+ | 1 | SIMPLE | tasks | ALL | NULL | NULL | NULL | NULL | 2 | | +----+-------------+-------+------+---------------+------+---------+------+------+-------+ 1 row in set (0.03 sec) => [#<Task id: 1, user_id: nil, project_id: 1, name: "W\r<br>T\r<br>F\r<br>VERY\r<br...", del eted: 0, done: 0, created_at: "2012-06-24 15:46:37", updated_at: "2012-06-30 17:13:27">, #<Task id: 2, user_id: nil, pro ject_id: 1, name: "Teste", deleted: 0, done: 0, created_at: "2012-06-30 17:11:27", updated_at: "2012-06-30 17:11:27">] I guess the project name should come too ? right ? What am I doing wrong ? Thank you -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en-US.
On Sat, Jun 30, 2012 at 10:36 AM, Felipe Pieretti Umpierre <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> irb(main):001:0> Task.all> => [#<Task id: 1, user_id: nil, project_id: 1, name: > "W\r<br>T\r<br>F\r<br>VERY\r<br...", del > eted: 0, done: 0, created_at: "2012-06-24 15:46:37", updated_at: > "2012-06-30 17:13:27">, #<Task id: 2, user_id: nil, pro > ject_id: 1, name: "Teste", deleted: 0, done: 0, created_at: "2012-06-30 > 17:11:27", updated_at: "2012-06-30 17:11:27">] > > I guess the project name should come too ? right ?Wrong.> What am I doing wrong ?Misunderstanding ActiveRecord and associations :-) Revisiting the relevant Rails guides might be a good idea. -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en-US.
On 30 June 2012 18:36, Felipe Pieretti Umpierre <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hello guys, I have two tables, tasks and projects, and each model I put: > > Task.rb > > class Task < ActiveRecord::Base > attr_accessible :user_id, :project_id, :name > belongs_to :project > end > > Project.rb > > class Project < ActiveRecord::Base > attr_accessible :name, :description > has_many :tasks > end > > But when I go to my prompt and I make a SELECT with task, none project > is returned. > > irb(main):001:0> Task.all > ←[1m←[36mTask Load (0.0ms)←[0m ←[1mSELECT `tasks`.* FROM `tasks` ←[0m > ←[1m←[35mEXPLAIN (30.0ms)←[0m EXPLAIN SELECT `tasks`.* FROM `tasks` > EXPLAIN for: SELECT `tasks`.* FROM `tasks` > +----+-------------+-------+------+---------------+------+---------+------+------+-------+ > | id | select_type | table | type | possible_keys | key | key_len | ref | rows | > Extra | > +----+-------------+-------+------+---------------+------+---------+------+------+-------+ > | 1 | SIMPLE | tasks | ALL | NULL | NULL | NULL | NULL | 2 | > | > +----+-------------+-------+------+---------------+------+---------+------+------+-------+ > 1 row in set (0.03 sec) > > => [#<Task id: 1, user_id: nil, project_id: 1, name: > "W\r<br>T\r<br>F\r<br>VERY\r<br...", del > eted: 0, done: 0, created_at: "2012-06-24 15:46:37", updated_at: > "2012-06-30 17:13:27">, #<Task id: 2, user_id: nil, pro > ject_id: 1, name: "Teste", deleted: 0, done: 0, created_at: "2012-06-30 > 17:11:27", updated_at: "2012-06-30 17:11:27">] > > I guess the project name should come too ? right ? > > What am I doing wrong ?Nothing. The project will only be fetched if you use :include in the query. However, for one of the tasks fetched above you can still say task.project and it will fetch the project then. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en-US.
Hello Colin, thank you for your answer.. I tryed this: irb(main):001:0> task = Task.all ←[1m←[36mTask Load (1.0ms)←[0m ←[1mSELECT `tasks`.* FROM `tasks` ←[0m ←[1m←[35mEXPLAIN (1.0ms)←[0m EXPLAIN SELECT `tasks`.* FROM `tasks` EXPLAIN for: SELECT `tasks`.* FROM `tasks` +----+-------------+-------+------+---------------+------+---------+------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+------+---------+------+------+-------+ | 1 | SIMPLE | tasks | ALL | NULL | NULL | NULL | NULL | 2 | | +----+-------------+-------+------+---------------+------+---------+------+------+-------+ 1 row in set (0.00 sec) => [#<Task id: 1, user_id: nil, project_id: 1, name: "W\r<br>T\r<br>F ?\r<br>VERY\r<br...", del eted: 0, done: 0, created_at: "2012-06-24 15:46:37", updated_at: "2012-06-30 17:13:27">, #<Task id: 2, user_id: nil, pro ject_id: 1, name: "Teste", deleted: 0, done: 0, created_at: "2012-06-30 17:11:27", updated_at: "2012-06-30 17:11:27">] irb(main):002:0> task.project NoMethodError: undefined method `project'' for #<Array:0x482b068> from (irb):2 from C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/commands/console.rb:47:in `start'' from C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in `start'' from C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/commands.rb:41:in `<top (required)>'' from script/rails:6:in `require'' from script/rails:6:in `<main>'' But when I try to call task.project a error shows up, and if I put task.id the same error is print Thank you -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en-US.
On Sat, Jun 30, 2012 at 4:17 PM, Felipe Pieretti Umpierre <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> irb(main):001:0> task = Task.allModel.all is clearly intended to return multiple results; it would be more appropriate to say `tasks = Task.all`> irb(main):002:0> task.project > NoMethodError: undefined method `project'' for #<Array:0x482b068>in which case you would be calling e.g. tasks.first.project or tasks.each{ |task| puts task.project.inspect } or whatever -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en-US.
Thanks a lot Hassan, I tryed with tasks.first.project and everything is OK. Thanks again. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en-US.