In Rails console how do I find records that below to a table
up vote
0
down vote
favorite
I have 3 models:
- Workspace (has many projects)
- Project (belongs to workspace and has many tasks)
- Task (belongs to project)
In the Rails console, I can do;
w = Workspace.find(1)
w.projects.all
This returns all of the projects that belong to workspace with Id = 1. How do o find all of the tasks that belong to workspace with Id = 1?
I have tried:
w.projects.tasks.all
but this doesn't work
ruby-on-rails
add a comment |
up vote
0
down vote
favorite
I have 3 models:
- Workspace (has many projects)
- Project (belongs to workspace and has many tasks)
- Task (belongs to project)
In the Rails console, I can do;
w = Workspace.find(1)
w.projects.all
This returns all of the projects that belong to workspace with Id = 1. How do o find all of the tasks that belong to workspace with Id = 1?
I have tried:
w.projects.tasks.all
but this doesn't work
ruby-on-rails
You can usehas_many :through
association. Ref: guides.rubyonrails.org/…
– Gokul M
yesterday
@GokulM worked perfectly. Thanks
– DRadnor
yesterday
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have 3 models:
- Workspace (has many projects)
- Project (belongs to workspace and has many tasks)
- Task (belongs to project)
In the Rails console, I can do;
w = Workspace.find(1)
w.projects.all
This returns all of the projects that belong to workspace with Id = 1. How do o find all of the tasks that belong to workspace with Id = 1?
I have tried:
w.projects.tasks.all
but this doesn't work
ruby-on-rails
I have 3 models:
- Workspace (has many projects)
- Project (belongs to workspace and has many tasks)
- Task (belongs to project)
In the Rails console, I can do;
w = Workspace.find(1)
w.projects.all
This returns all of the projects that belong to workspace with Id = 1. How do o find all of the tasks that belong to workspace with Id = 1?
I have tried:
w.projects.tasks.all
but this doesn't work
ruby-on-rails
ruby-on-rails
edited yesterday
Sergio Tulentsev
176k29286300
176k29286300
asked yesterday
DRadnor
31
31
You can usehas_many :through
association. Ref: guides.rubyonrails.org/…
– Gokul M
yesterday
@GokulM worked perfectly. Thanks
– DRadnor
yesterday
add a comment |
You can usehas_many :through
association. Ref: guides.rubyonrails.org/…
– Gokul M
yesterday
@GokulM worked perfectly. Thanks
– DRadnor
yesterday
You can use
has_many :through
association. Ref: guides.rubyonrails.org/…– Gokul M
yesterday
You can use
has_many :through
association. Ref: guides.rubyonrails.org/…– Gokul M
yesterday
@GokulM worked perfectly. Thanks
– DRadnor
yesterday
@GokulM worked perfectly. Thanks
– DRadnor
yesterday
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
In your workspace
model use the below association:
has_many :tasks, through: :projects
Also, I recommend you to check the Rails guides
add a comment |
up vote
0
down vote
As per the description mentioned in the post you need to find the task belongs to a particular project that in turn belongs to a particular workspace.
w.projects.tasks.all
The above line won't be working as w.projects would returning collection of ActiveRecord and for association to work you need an object.
Assuming you have written necessary association as mentioned by @Abhilash, below mentioned query would achieve would you need.
#1st solution
project_tasks = w.includes(:projects => [:tasks]) #eager-loading both project and tasks
#2nd solution
project_tasks = w.projects.map {|project| project.tasks } # will return all tasks
Hope it clears your query!!
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
In your workspace
model use the below association:
has_many :tasks, through: :projects
Also, I recommend you to check the Rails guides
add a comment |
up vote
0
down vote
In your workspace
model use the below association:
has_many :tasks, through: :projects
Also, I recommend you to check the Rails guides
add a comment |
up vote
0
down vote
up vote
0
down vote
In your workspace
model use the below association:
has_many :tasks, through: :projects
Also, I recommend you to check the Rails guides
In your workspace
model use the below association:
has_many :tasks, through: :projects
Also, I recommend you to check the Rails guides
answered yesterday
Abhilash Reddy
927616
927616
add a comment |
add a comment |
up vote
0
down vote
As per the description mentioned in the post you need to find the task belongs to a particular project that in turn belongs to a particular workspace.
w.projects.tasks.all
The above line won't be working as w.projects would returning collection of ActiveRecord and for association to work you need an object.
Assuming you have written necessary association as mentioned by @Abhilash, below mentioned query would achieve would you need.
#1st solution
project_tasks = w.includes(:projects => [:tasks]) #eager-loading both project and tasks
#2nd solution
project_tasks = w.projects.map {|project| project.tasks } # will return all tasks
Hope it clears your query!!
add a comment |
up vote
0
down vote
As per the description mentioned in the post you need to find the task belongs to a particular project that in turn belongs to a particular workspace.
w.projects.tasks.all
The above line won't be working as w.projects would returning collection of ActiveRecord and for association to work you need an object.
Assuming you have written necessary association as mentioned by @Abhilash, below mentioned query would achieve would you need.
#1st solution
project_tasks = w.includes(:projects => [:tasks]) #eager-loading both project and tasks
#2nd solution
project_tasks = w.projects.map {|project| project.tasks } # will return all tasks
Hope it clears your query!!
add a comment |
up vote
0
down vote
up vote
0
down vote
As per the description mentioned in the post you need to find the task belongs to a particular project that in turn belongs to a particular workspace.
w.projects.tasks.all
The above line won't be working as w.projects would returning collection of ActiveRecord and for association to work you need an object.
Assuming you have written necessary association as mentioned by @Abhilash, below mentioned query would achieve would you need.
#1st solution
project_tasks = w.includes(:projects => [:tasks]) #eager-loading both project and tasks
#2nd solution
project_tasks = w.projects.map {|project| project.tasks } # will return all tasks
Hope it clears your query!!
As per the description mentioned in the post you need to find the task belongs to a particular project that in turn belongs to a particular workspace.
w.projects.tasks.all
The above line won't be working as w.projects would returning collection of ActiveRecord and for association to work you need an object.
Assuming you have written necessary association as mentioned by @Abhilash, below mentioned query would achieve would you need.
#1st solution
project_tasks = w.includes(:projects => [:tasks]) #eager-loading both project and tasks
#2nd solution
project_tasks = w.projects.map {|project| project.tasks } # will return all tasks
Hope it clears your query!!
answered yesterday
Rohan
9641311
9641311
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53349285%2fin-rails-console-how-do-i-find-records-that-below-to-a-table%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You can use
has_many :through
association. Ref: guides.rubyonrails.org/…– Gokul M
yesterday
@GokulM worked perfectly. Thanks
– DRadnor
yesterday