게시물을 작성할 양식이 있습니다. 이 양식에는 게시물에 태그가 있으며 사용자가 소유합니다. 양식에서 가상 속성 인 tag_ids를 사용하고 있습니다.모델에서 current_user를 사용하지 않는 방법
나는 find_or_create_by_name_and_user_id를 입력 한 다음 current_user.id를 전달하는 것과 같이이 작업을하고 싶습니다.
def tag_ids=(tags_string)
self.taggings.destroy_all
tag_names = tags_string.split(",").collect{|s| s.strip.downcase}.uniq
tag_names.each do |tag_name|
tag = Tag.find_or_create_by_name_and_user_id(tag_name, current_user.id)
tagging = self.taggings.new
tagging.tag_id = tag.id
end
end
내가 읽은 바로는 그것이 어떻게 작동해야하는가 아닙니다. 현재 사용자는 PostsContoller create 메소드에서 호출되어야합니다. 내가 붙어있어 어디
def create
@post = Post.new(params[:post])
@post.user = current_user
respond_to do |format|
if params[:commit]
@post.save
... 끝
이다.params가 모든 속성을 전달하는 경우 : name 및 : user_id 속성이있는 태그 모델에 current_user id를 삽입하려면 어떻게해야합니까?
나는 또한 나의 협회 때문에 이것이 작동해야한다는 것을 읽었습니다.
사용자 - has_many posts; has_many 태그
포스트 - belongs_to 사용자; has_many 태깅; taggings을 통해 has_many 태그
태그 - 많은 태깅이 있습니다. taggings을 통해 has_many 게시물; belongs_to user
태그 지정 - belongs_to 게시물; 태그
저에게 적합하지 않습니다. 나는 "네임에 대한 호출 된 ID는 4가 될 것입니다. 실수로 id가 필요하다면 object_id를 사용하십시오."이것은 self.user.id에 있습니다. – ebbflowgo
self.user의 값은 무엇입니까 – Amar
self.user is nil – ebbflowgo