레일의 지오 코더와의 연결을 통해 검색 할 수 없음 activeRecord :: relation :: activerecord_relation 오류를 방문 할 수 없습니다.
-
20-12-2019 - |
문제
등록시 지오 코더 보석을 사용하여 지오 코더 보석을 사용하여 주소가 지오 코딩 된 포스터 (사용자 유형)에 속한 게시자 자원이 있습니다.이제는 사용자가 거리별로 근처의 게시물을 검색 할 수 있기를 바랍니다.나는 비교적 새롭게 새로운 것입니다. 어떤 도움이 크게 감사 할 것입니다.
다음과 같은 오류가 발생합니다 (나는 그것이 무엇을 의미하는지 알지 못한다, 나는 내 모델에서 연관성을 충분히 설정한다고 생각했다)
Cannot visit ActiveRecord::Relation::ActiveRecord_Relation_Poster
.
index.html.erb
<%= form_tag posts_path, method: :get do %>
Find postings <%= select_tag "within", options_for_select([["Next door", 0.001],["In your neighborhood", 0.05], ["Nearby", 0.1]], ["Nearby", 0.1]) %>
<%= submit_tag "Find", :name => nil %>
<% end %>
.
posts_controller.erb
def index
if params[:within]
@posts= Post.where(Poster.near(current_user, params[:within].to_f))
else
@posts=Post.all
end
end
.
포스트 모델
class Post < ActiveRecord::Base
belongs_to :poster
end
.
포스터 모델
class Poster < User
has_many :posts, dependent: :destroy
end
.
사용자 모델
class User < ActiveRecord::Base
def full_address
[address_one, city, state, zip].compact.join(', ')
end
geocoded_by :full_address, :latitude => :lat, :longitude => :long
after_validation :geocode, if: -> (full_address){ full_address.present? or address_changed? }
end
. 해결책
방금 게시물 작성에 대한 속성을지나갔습니다. 원래의 문제를 해결할 방법이 있었으면 좋겠습니다 (데이터베이스를 덜 중복으로 만들어줍니다)
내 게시물 컨트롤러
def create
@post = current_user.posts.build(post_params)
@post.assign_attributes(:lat => current_user.lat, :long => current_user.long)
end
def index
if params[:within]
@posts= Post.near(current_user, params[:within].to_f)
else
@posts=Post.all
end
end
.
또한 내 게시물 모델에서 "지오 코딩 작성자 : 포스터"를 사용했습니다
제휴하지 않습니다 StackOverflow