Rails with Searchkick: getting results with a joined table
I'm starting to use searchkick in my app, and I have two models: House and Address (which belongs to a house).
House:
class House < ApplicationRecord
searchkick
has_one :address, dependent: :destroy
end
Address:
class Address < ApplicationRecord
searchkick
belongs_to :house
end
In my controller I have
def index
if params[:term].present?
@houses = House.search(params[:term])
else
@houses = House.search('*')
end
end
Right now, searchkick is working when I search for house data which is in the table houses
, but if for example I look for a field in the Addresses table, I do not get any result at all.
Is it possible to do this?
Thanks a lot in advance!
ruby-on-rails elasticsearch searchkick
add a comment |
I'm starting to use searchkick in my app, and I have two models: House and Address (which belongs to a house).
House:
class House < ApplicationRecord
searchkick
has_one :address, dependent: :destroy
end
Address:
class Address < ApplicationRecord
searchkick
belongs_to :house
end
In my controller I have
def index
if params[:term].present?
@houses = House.search(params[:term])
else
@houses = House.search('*')
end
end
Right now, searchkick is working when I search for house data which is in the table houses
, but if for example I look for a field in the Addresses table, I do not get any result at all.
Is it possible to do this?
Thanks a lot in advance!
ruby-on-rails elasticsearch searchkick
add a comment |
I'm starting to use searchkick in my app, and I have two models: House and Address (which belongs to a house).
House:
class House < ApplicationRecord
searchkick
has_one :address, dependent: :destroy
end
Address:
class Address < ApplicationRecord
searchkick
belongs_to :house
end
In my controller I have
def index
if params[:term].present?
@houses = House.search(params[:term])
else
@houses = House.search('*')
end
end
Right now, searchkick is working when I search for house data which is in the table houses
, but if for example I look for a field in the Addresses table, I do not get any result at all.
Is it possible to do this?
Thanks a lot in advance!
ruby-on-rails elasticsearch searchkick
I'm starting to use searchkick in my app, and I have two models: House and Address (which belongs to a house).
House:
class House < ApplicationRecord
searchkick
has_one :address, dependent: :destroy
end
Address:
class Address < ApplicationRecord
searchkick
belongs_to :house
end
In my controller I have
def index
if params[:term].present?
@houses = House.search(params[:term])
else
@houses = House.search('*')
end
end
Right now, searchkick is working when I search for house data which is in the table houses
, but if for example I look for a field in the Addresses table, I do not get any result at all.
Is it possible to do this?
Thanks a lot in advance!
ruby-on-rails elasticsearch searchkick
ruby-on-rails elasticsearch searchkick
edited Nov 25 '18 at 10:27
noloman
asked Nov 24 '18 at 16:34
nolomannoloman
5,344136393
5,344136393
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I think you need to index addresses also. To do that add the below code to your House
model:
def search_data
attributes.merge(
address_city: self.address.try(:city),
address_zip: self.address.try(:zip)
)
end
Feel free to modify the above code based on your fields in the Address
model.
Also, after adding the above code, don't forget to reindex.
well it isn't really working:undefined local variable or method
addresses' for #<Class:0x00007fb78071ddd0>`, how can I do that? I didn't know I could use other models inside a model class
– noloman
Nov 25 '18 at 10:20
That was just an example. To give you an exact answer, You will have to provide us with the associations you have defined in yourHouse
model.
– Abhilash Reddy
Nov 25 '18 at 10:22
and also the columns you've in the other table.
– Abhilash Reddy
Nov 25 '18 at 10:23
I edited my question with the models, thanks a lot!
– noloman
Nov 25 '18 at 10:27
@noloman Updated the answer. Since you've not provided the fields available inaddress
model, I've assumed that you will have city and zip in addresses table
– Abhilash Reddy
Nov 25 '18 at 10:38
|
show 6 more comments
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%2f53460192%2frails-with-searchkick-getting-results-with-a-joined-table%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
I think you need to index addresses also. To do that add the below code to your House
model:
def search_data
attributes.merge(
address_city: self.address.try(:city),
address_zip: self.address.try(:zip)
)
end
Feel free to modify the above code based on your fields in the Address
model.
Also, after adding the above code, don't forget to reindex.
well it isn't really working:undefined local variable or method
addresses' for #<Class:0x00007fb78071ddd0>`, how can I do that? I didn't know I could use other models inside a model class
– noloman
Nov 25 '18 at 10:20
That was just an example. To give you an exact answer, You will have to provide us with the associations you have defined in yourHouse
model.
– Abhilash Reddy
Nov 25 '18 at 10:22
and also the columns you've in the other table.
– Abhilash Reddy
Nov 25 '18 at 10:23
I edited my question with the models, thanks a lot!
– noloman
Nov 25 '18 at 10:27
@noloman Updated the answer. Since you've not provided the fields available inaddress
model, I've assumed that you will have city and zip in addresses table
– Abhilash Reddy
Nov 25 '18 at 10:38
|
show 6 more comments
I think you need to index addresses also. To do that add the below code to your House
model:
def search_data
attributes.merge(
address_city: self.address.try(:city),
address_zip: self.address.try(:zip)
)
end
Feel free to modify the above code based on your fields in the Address
model.
Also, after adding the above code, don't forget to reindex.
well it isn't really working:undefined local variable or method
addresses' for #<Class:0x00007fb78071ddd0>`, how can I do that? I didn't know I could use other models inside a model class
– noloman
Nov 25 '18 at 10:20
That was just an example. To give you an exact answer, You will have to provide us with the associations you have defined in yourHouse
model.
– Abhilash Reddy
Nov 25 '18 at 10:22
and also the columns you've in the other table.
– Abhilash Reddy
Nov 25 '18 at 10:23
I edited my question with the models, thanks a lot!
– noloman
Nov 25 '18 at 10:27
@noloman Updated the answer. Since you've not provided the fields available inaddress
model, I've assumed that you will have city and zip in addresses table
– Abhilash Reddy
Nov 25 '18 at 10:38
|
show 6 more comments
I think you need to index addresses also. To do that add the below code to your House
model:
def search_data
attributes.merge(
address_city: self.address.try(:city),
address_zip: self.address.try(:zip)
)
end
Feel free to modify the above code based on your fields in the Address
model.
Also, after adding the above code, don't forget to reindex.
I think you need to index addresses also. To do that add the below code to your House
model:
def search_data
attributes.merge(
address_city: self.address.try(:city),
address_zip: self.address.try(:zip)
)
end
Feel free to modify the above code based on your fields in the Address
model.
Also, after adding the above code, don't forget to reindex.
edited Nov 25 '18 at 10:46
answered Nov 24 '18 at 17:48
Abhilash ReddyAbhilash Reddy
1,1401618
1,1401618
well it isn't really working:undefined local variable or method
addresses' for #<Class:0x00007fb78071ddd0>`, how can I do that? I didn't know I could use other models inside a model class
– noloman
Nov 25 '18 at 10:20
That was just an example. To give you an exact answer, You will have to provide us with the associations you have defined in yourHouse
model.
– Abhilash Reddy
Nov 25 '18 at 10:22
and also the columns you've in the other table.
– Abhilash Reddy
Nov 25 '18 at 10:23
I edited my question with the models, thanks a lot!
– noloman
Nov 25 '18 at 10:27
@noloman Updated the answer. Since you've not provided the fields available inaddress
model, I've assumed that you will have city and zip in addresses table
– Abhilash Reddy
Nov 25 '18 at 10:38
|
show 6 more comments
well it isn't really working:undefined local variable or method
addresses' for #<Class:0x00007fb78071ddd0>`, how can I do that? I didn't know I could use other models inside a model class
– noloman
Nov 25 '18 at 10:20
That was just an example. To give you an exact answer, You will have to provide us with the associations you have defined in yourHouse
model.
– Abhilash Reddy
Nov 25 '18 at 10:22
and also the columns you've in the other table.
– Abhilash Reddy
Nov 25 '18 at 10:23
I edited my question with the models, thanks a lot!
– noloman
Nov 25 '18 at 10:27
@noloman Updated the answer. Since you've not provided the fields available inaddress
model, I've assumed that you will have city and zip in addresses table
– Abhilash Reddy
Nov 25 '18 at 10:38
well it isn't really working:
undefined local variable or method
addresses' for #<Class:0x00007fb78071ddd0>`, how can I do that? I didn't know I could use other models inside a model class– noloman
Nov 25 '18 at 10:20
well it isn't really working:
undefined local variable or method
addresses' for #<Class:0x00007fb78071ddd0>`, how can I do that? I didn't know I could use other models inside a model class– noloman
Nov 25 '18 at 10:20
That was just an example. To give you an exact answer, You will have to provide us with the associations you have defined in your
House
model.– Abhilash Reddy
Nov 25 '18 at 10:22
That was just an example. To give you an exact answer, You will have to provide us with the associations you have defined in your
House
model.– Abhilash Reddy
Nov 25 '18 at 10:22
and also the columns you've in the other table.
– Abhilash Reddy
Nov 25 '18 at 10:23
and also the columns you've in the other table.
– Abhilash Reddy
Nov 25 '18 at 10:23
I edited my question with the models, thanks a lot!
– noloman
Nov 25 '18 at 10:27
I edited my question with the models, thanks a lot!
– noloman
Nov 25 '18 at 10:27
@noloman Updated the answer. Since you've not provided the fields available in
address
model, I've assumed that you will have city and zip in addresses table– Abhilash Reddy
Nov 25 '18 at 10:38
@noloman Updated the answer. Since you've not provided the fields available in
address
model, I've assumed that you will have city and zip in addresses table– Abhilash Reddy
Nov 25 '18 at 10:38
|
show 6 more comments
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%2f53460192%2frails-with-searchkick-getting-results-with-a-joined-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