Show results depending on association2019 Community Moderator ElectionNameError in CompetitorsController#indexmultiple joins in railsHow to prevent Rails 2.3.8 from checking all dependencies while loadingdynamic nested form always creates an extra blank entry - using formtastic_coocoonwhere does rails get the queries for nested objects?Cant get model working for submit and validation on the show pageHow do I not use current_user in the modelRails 3.2.9 accepts_nested_attributes_for confusionGroup by entire field value in Tire/ElasticSearchHow to validate request parameters in rails 4 like laravel 5?
Why is this plane circling around the Lucknow airport every day?
MTG: Can I kill an opponent in response to lethal activated abilities, and not take the damage?
Finding algorithms of QGIS commands?
Why would a jet engine that runs at temps excess of 2000°C burn when it crashes?
They call me Inspector Morse
Can you reject a postdoc offer after the PI has paid a large sum for flights/accommodation for your visit?
Look through the portal of every day
"One can do his homework in the library"
infinitive telling the purpose
Is there a window switcher for GNOME that shows the actual window?
How do I express some one as a black person?
Accountant/ lawyer will not return my call
Space in array system equations
The bar has been raised
Latest web browser compatible with Windows 98
Should I take out a loan for a friend to invest on my behalf?
In the late 1940’s to early 1950’s what technology was available that could melt a LOT of ice?
Solving "Resistance between two nodes on a grid" problem in Mathematica
Low budget alien movie about the Earth being cooked
How to create a hard link to an inode (ext4)?
Is it true that real estate prices mainly go up?
Why is Beresheet doing a only a one-way trip?
Grey hair or white hair
PTIJ: Why can't I eat anything?
Show results depending on association
2019 Community Moderator ElectionNameError in CompetitorsController#indexmultiple joins in railsHow to prevent Rails 2.3.8 from checking all dependencies while loadingdynamic nested form always creates an extra blank entry - using formtastic_coocoonwhere does rails get the queries for nested objects?Cant get model working for submit and validation on the show pageHow do I not use current_user in the modelRails 3.2.9 accepts_nested_attributes_for confusionGroup by entire field value in Tire/ElasticSearchHow to validate request parameters in rails 4 like laravel 5?
I am building a landlord mgt application. But I am having an issue - I only want to index requests where the house.id
(association value) matches the current user.
Currently, I have got it to work depending if the user has created it. But I would like the landlord to see all requests that are related to their properties.
requests_controller.rb:
def index
unless current_user.estate_agent?
@requests = Request.where(user_id: current_user).search(params[:search])
else
@requests = Request.search(params[:search])
end
end
request.rb
class Request < ActiveRecord::Base
belongs_to :user
belongs_to :house
def self.search(search)
key = "%#search%"
if search
where('description LIKE ? OR title LIKE ?', key, key)
else
all
end
end
end
house.rb
class House < ActiveRecord::Base
belongs_to :user
belongs_to :tenant
has_many :requests
belongs_to :contract
mount_uploader :energy, EnergyUploader
validates_presence_of :house_title, :description, :doorno, :postcode, :price, :bedroom, :house_type
def full_house_name
"#doorno #house_title"
end
def self.search(search)
key = "%#search%"
if search
where('doorno LIKE ? OR house_title LIKE ? OR price LIKE ? OR postcode LIKE ? OR house_type LIKE ? OR bedroom LIKE ?', key, key, key, key, key, key)
else
all
end
end
end
ruby-on-rails ruby sqlite
add a comment |
I am building a landlord mgt application. But I am having an issue - I only want to index requests where the house.id
(association value) matches the current user.
Currently, I have got it to work depending if the user has created it. But I would like the landlord to see all requests that are related to their properties.
requests_controller.rb:
def index
unless current_user.estate_agent?
@requests = Request.where(user_id: current_user).search(params[:search])
else
@requests = Request.search(params[:search])
end
end
request.rb
class Request < ActiveRecord::Base
belongs_to :user
belongs_to :house
def self.search(search)
key = "%#search%"
if search
where('description LIKE ? OR title LIKE ?', key, key)
else
all
end
end
end
house.rb
class House < ActiveRecord::Base
belongs_to :user
belongs_to :tenant
has_many :requests
belongs_to :contract
mount_uploader :energy, EnergyUploader
validates_presence_of :house_title, :description, :doorno, :postcode, :price, :bedroom, :house_type
def full_house_name
"#doorno #house_title"
end
def self.search(search)
key = "%#search%"
if search
where('doorno LIKE ? OR house_title LIKE ? OR price LIKE ? OR postcode LIKE ? OR house_type LIKE ? OR bedroom LIKE ?', key, key, key, key, key, key)
else
all
end
end
end
ruby-on-rails ruby sqlite
can you show your model relationship
– widjajayd
Mar 6 at 16:46
1
Try not to do anunless
with anelse
, as that's a double negative. Instead use anif
and reverse the order of the blocks. You'll also want to indent your code properly as right now it's all jumbled and hard to read, structurally speaking.
– tadman
Mar 6 at 16:54
@widjajayd added both models, thanks
– Kush
Mar 6 at 16:54
add a comment |
I am building a landlord mgt application. But I am having an issue - I only want to index requests where the house.id
(association value) matches the current user.
Currently, I have got it to work depending if the user has created it. But I would like the landlord to see all requests that are related to their properties.
requests_controller.rb:
def index
unless current_user.estate_agent?
@requests = Request.where(user_id: current_user).search(params[:search])
else
@requests = Request.search(params[:search])
end
end
request.rb
class Request < ActiveRecord::Base
belongs_to :user
belongs_to :house
def self.search(search)
key = "%#search%"
if search
where('description LIKE ? OR title LIKE ?', key, key)
else
all
end
end
end
house.rb
class House < ActiveRecord::Base
belongs_to :user
belongs_to :tenant
has_many :requests
belongs_to :contract
mount_uploader :energy, EnergyUploader
validates_presence_of :house_title, :description, :doorno, :postcode, :price, :bedroom, :house_type
def full_house_name
"#doorno #house_title"
end
def self.search(search)
key = "%#search%"
if search
where('doorno LIKE ? OR house_title LIKE ? OR price LIKE ? OR postcode LIKE ? OR house_type LIKE ? OR bedroom LIKE ?', key, key, key, key, key, key)
else
all
end
end
end
ruby-on-rails ruby sqlite
I am building a landlord mgt application. But I am having an issue - I only want to index requests where the house.id
(association value) matches the current user.
Currently, I have got it to work depending if the user has created it. But I would like the landlord to see all requests that are related to their properties.
requests_controller.rb:
def index
unless current_user.estate_agent?
@requests = Request.where(user_id: current_user).search(params[:search])
else
@requests = Request.search(params[:search])
end
end
request.rb
class Request < ActiveRecord::Base
belongs_to :user
belongs_to :house
def self.search(search)
key = "%#search%"
if search
where('description LIKE ? OR title LIKE ?', key, key)
else
all
end
end
end
house.rb
class House < ActiveRecord::Base
belongs_to :user
belongs_to :tenant
has_many :requests
belongs_to :contract
mount_uploader :energy, EnergyUploader
validates_presence_of :house_title, :description, :doorno, :postcode, :price, :bedroom, :house_type
def full_house_name
"#doorno #house_title"
end
def self.search(search)
key = "%#search%"
if search
where('doorno LIKE ? OR house_title LIKE ? OR price LIKE ? OR postcode LIKE ? OR house_type LIKE ? OR bedroom LIKE ?', key, key, key, key, key, key)
else
all
end
end
end
ruby-on-rails ruby sqlite
ruby-on-rails ruby sqlite
edited Mar 7 at 7:37
Vasilisa
2,97521120
2,97521120
asked Mar 6 at 16:42
KushKush
105
105
can you show your model relationship
– widjajayd
Mar 6 at 16:46
1
Try not to do anunless
with anelse
, as that's a double negative. Instead use anif
and reverse the order of the blocks. You'll also want to indent your code properly as right now it's all jumbled and hard to read, structurally speaking.
– tadman
Mar 6 at 16:54
@widjajayd added both models, thanks
– Kush
Mar 6 at 16:54
add a comment |
can you show your model relationship
– widjajayd
Mar 6 at 16:46
1
Try not to do anunless
with anelse
, as that's a double negative. Instead use anif
and reverse the order of the blocks. You'll also want to indent your code properly as right now it's all jumbled and hard to read, structurally speaking.
– tadman
Mar 6 at 16:54
@widjajayd added both models, thanks
– Kush
Mar 6 at 16:54
can you show your model relationship
– widjajayd
Mar 6 at 16:46
can you show your model relationship
– widjajayd
Mar 6 at 16:46
1
1
Try not to do an
unless
with an else
, as that's a double negative. Instead use an if
and reverse the order of the blocks. You'll also want to indent your code properly as right now it's all jumbled and hard to read, structurally speaking.– tadman
Mar 6 at 16:54
Try not to do an
unless
with an else
, as that's a double negative. Instead use an if
and reverse the order of the blocks. You'll also want to indent your code properly as right now it's all jumbled and hard to read, structurally speaking.– tadman
Mar 6 at 16:54
@widjajayd added both models, thanks
– Kush
Mar 6 at 16:54
@widjajayd added both models, thanks
– Kush
Mar 6 at 16:54
add a comment |
1 Answer
1
active
oldest
votes
If I understood you correctly, you want to show all requests that were made by the user or made for his real-state.
The code below will create your initial filter, then you can call your search
method over it.
Request.where(user_id: current_user.id).or(Request.where(house: user_id: current_user.id))
Thanks @merehghost. I tried this but get a undefined error on the (or) part
– Kush
yesterday
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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%2f55028140%2fshow-results-depending-on-association%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If I understood you correctly, you want to show all requests that were made by the user or made for his real-state.
The code below will create your initial filter, then you can call your search
method over it.
Request.where(user_id: current_user.id).or(Request.where(house: user_id: current_user.id))
Thanks @merehghost. I tried this but get a undefined error on the (or) part
– Kush
yesterday
add a comment |
If I understood you correctly, you want to show all requests that were made by the user or made for his real-state.
The code below will create your initial filter, then you can call your search
method over it.
Request.where(user_id: current_user.id).or(Request.where(house: user_id: current_user.id))
Thanks @merehghost. I tried this but get a undefined error on the (or) part
– Kush
yesterday
add a comment |
If I understood you correctly, you want to show all requests that were made by the user or made for his real-state.
The code below will create your initial filter, then you can call your search
method over it.
Request.where(user_id: current_user.id).or(Request.where(house: user_id: current_user.id))
If I understood you correctly, you want to show all requests that were made by the user or made for his real-state.
The code below will create your initial filter, then you can call your search
method over it.
Request.where(user_id: current_user.id).or(Request.where(house: user_id: current_user.id))
answered Mar 7 at 10:11
MereghostMereghost
61847
61847
Thanks @merehghost. I tried this but get a undefined error on the (or) part
– Kush
yesterday
add a comment |
Thanks @merehghost. I tried this but get a undefined error on the (or) part
– Kush
yesterday
Thanks @merehghost. I tried this but get a undefined error on the (or) part
– Kush
yesterday
Thanks @merehghost. I tried this but get a undefined error on the (or) part
– Kush
yesterday
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f55028140%2fshow-results-depending-on-association%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
can you show your model relationship
– widjajayd
Mar 6 at 16:46
1
Try not to do an
unless
with anelse
, as that's a double negative. Instead use anif
and reverse the order of the blocks. You'll also want to indent your code properly as right now it's all jumbled and hard to read, structurally speaking.– tadman
Mar 6 at 16:54
@widjajayd added both models, thanks
– Kush
Mar 6 at 16:54