Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【Question】When use colon(::) for class name. #4

Closed
kntmrkm opened this issue Jul 10, 2014 · 6 comments
Closed

【Question】When use colon(::) for class name. #4

kntmrkm opened this issue Jul 10, 2014 · 6 comments

Comments

@kntmrkm
Copy link

kntmrkm commented Jul 10, 2014

Hi there.

  • Rails 4.1.2
  • attr_searchable 0.0.4

my setting is below..

models

  • Account
  • Setting

Account

class Account < ActiveRecord::Base
  attr_searchable :name
  attr_searchable setting: ["account_settings.formal_name"]
  has_one :setting
end

Setting

database tablename is "account_settings"

class Account::Setting < ActiveRecord::Base

end

Directory structure is ...

  • app/models
    • account
      • setting.rb
    • account.rb

I tried. but NameError happened.

uninitialized constant AccountSetting

How do I write to model setting?

@mrkamel
Copy link
Owner

mrkamel commented Jul 10, 2014

Hi, thanks for posting.

I added a patch to make #attr_searchable_alias support this case.
Could you add the upstream version to your Gemfile:

gem 'attr_searchable', :git => 'https://github.com/mrkamel/attr_searchable.git'

and check if the following is sufficient for you:

class Account < ActiveRecord::Base
  include AttrSearchable

  attr_searchable :name
  attr_searchable :setting => "account_settings.formal_name"

  attr_searchable_alias :account_settings => :setting

  has_one :setting
end

@kntmrkm
Copy link
Author

kntmrkm commented Jul 10, 2014

Thanks for response.
I tried.
but... error happend.

PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "account_settings"
LINE 1: ..." != 0) AND (("accounts"."name" ILIKE '%柔術%' OR "account_s...
                                                             ^
: SELECT  "accounts".* FROM "accounts"  WHERE ("accounts"."id" != 0) AND (("accounts"."name" ILIKE '%柔術%' OR "account_settings"."formal_name" ILIKE '%柔術%')) LIMIT 25 OFFSET 0
  Account Load (0.6ms)  
    SELECT
        "accounts" . *
    FROM
        "accounts"
    WHERE
        (
            "accounts" . "id" != 0
        )
        AND (
            (
                "accounts" . "name" ILIKE '%柔術%'
                OR "account_settings" . "formal_name" ILIKE '%柔術%'
            )
        ) LIMIT 25 OFFSET 0

I have make sure existense of "account_settings".
my mistake?

Loading development environment (Rails 4.1.2)
4.1.2@2.1.2 (main)> Account::Setting.all.to_sql
=> "SELECT \"account_settings\".* FROM \"account_settings\""

part of code.

class Account < Common
  include AttrSearchable
  attr_searchable :name
  attr_searchable :setting => "account_settings.formal_name"

  attr_searchable_alias :account_settings => :setting
  scope :search_scope, -> {  }

  has_one :setting, dependent: :destroy
end

@mrkamel
Copy link
Owner

mrkamel commented Jul 10, 2014

This error does not belong to the primary issue.

An empty :search_scope can't be sufficient here, because you want to search within associations. You rather have to not define a scope :search_scope to let AttrSearchable auto-eagerload the associations - or - manually load the associations you're searching in

scope :search_scope, -> { eager_load :setting }

# OR

scope :search_scope, -> { joins :setting }

# OR arbitrary other definitions

# OR not use search_scope at all to let AttrSearchable auto-eagerload :setting

@kntmrkm
Copy link
Author

kntmrkm commented Jul 10, 2014

Very Sorry.....

I got it.

I used scope :search_scope, -> { }.
because, when I search for only main model (account), a gem "bullet" issue warning below.

Unused Eager Loading detected
  Account => [[]]
  Remove from your finder: :include => [:[]]

So, I tried empty search_scope.

Anyway. very thanks.

and sorry my English.

@kntmrkm kntmrkm closed this as completed Jul 10, 2014
@mrkamel
Copy link
Owner

mrkamel commented Jul 10, 2014

Nothing to worry about.

Moreover, it's already on my agenda to extend the auto-eagerloading to only eager load what is really neccessary (only the associations referenced within the query string), such that the warning will go away soon.

@kntmrkm
Copy link
Author

kntmrkm commented Jul 10, 2014

It's pretty good!
I'm looking forward to it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants