何を書けばいいのか悩んだ末のact-as-taggable-on

初めまして!
7月からinterfirmでインターンをさせていただいている榊間です!

これから開発部プログを書いていこうということで、文章を書くスキルがほとんどないですが(ツイッターが限界)、これから週一回(たぶん)投稿させていただきます。

まず、僕の自己紹介から...
関西の大学の四年生で、今は東京に住んでインターン
初めてプログラミングをやったのが2月頃(PHP)で、rubyを学び始めたのは6月になってから

っていう感じなので、新しく覚えたことなんかを投稿していこうかと思ってます。

とりあえず最近やったタスクの中にあった、タグ管理のgemのひとつ、act-as-taggable-on(これしか書けそうになかった)について書いときます。



使い方

act-as-taggable-on(mbleigh/acts-as-taggable-on · GitHub)をインストールしたら

rake acts_as_taggable_on_engine:install:migrations
rake db:migrate

でタグ管理用のテーブルを勝手に作ってくれます。それから、タグを取り付けたいモデル(この場合はArticle)に以下のように記述します。

class Article < ActiveRecord::Base
  acts_as_taggable
end

これでタグを管理する準備が終わります。後はGithubにあるようなメソッドを使うだけで、タグの追加や削除などなどが行われます。とっても簡単です。



このact-as-taggable-onを使っていた時にちょっとつまずいたとこ

articlesの内容を検索する時に、articlesのテーブル内のカラム(例えばtitle)に加えて、tagにも検索をかけたい場合、whereで一緒に検索できない  ↓ダメな例

@articles = Article.where(['title LIKE ? or tag LIKE ? ', "%#{params[:search]}%","%#{params[:search]}%"])

(検索で入力したワードをparams[:search]で渡しているとする)
これは考えてみれば当たり前で、Articleモデルに関連付けているtagはそもそもarticlesテーブルに存在しておらず、最初に生成されたテーブルで管理されているからです。

ここでGithubに戻ってtagの検索方法を調べてみると、タグが付いているArticleをタグ検索(LIKE検索)したい場合は、下記のようにします。

@articles = Article.tagged_with(params[:search], :wild => true, :any => true)

じゃあこれを、titleカラムの検索と同時にしようとすると

@articles = Article.tagged_with(params[:search], :wild => true, :any => true) | Articles.where(['title LIKE ? ', "%#{params[:search]}%"])

こんな感じで、漏れなく検索できるようになりました!

後気をつけることは、この時の@articlesはもうActiveRecordクラスのメソッドが使えなくなるので、例えば並び替えの時とかは、orderじゃなくてsort_byとか使わなきゃいけないみたいです、、

ここ参考にしました
Railsでacts-as-taggable-onを使ってタグ管理を行う - Rails Webook



また学んだこととか書いていくのでよろしくお願いしますm(__)m

My Final Week in Interfirm

こんにちは、ロイです。

This is my final week in Interfirm, I will go back to China this Sunday for my univerisity graduation.

Since March 4th I have spent two and a half months in Interfirm. I can still remember the first day I came into the company, when I introduce myself to all people in the company. Time passes by so rapidly, and I learnt a lot from Interfirm, not only the developing skills but also Japanese Culture. Japanese company is quite different from Chinese company, different culture and habit, accept the different and learn from it makes working more meaningful.

During this time, I learnt how to use ruby and rails, and work on Cartube with Mahdi. Then I could have chance to build my own service in one month. It was really interesting experience that I could have chance to realize what I want to do in company, and try some advanced developing tools.

Interfirm is a great company, it has good products and nice working environment. I hope Interfirm would become Japanese No.1 media platform.

Thanks for everyone in the company helped me a lot during the three months, I really appreciate that.

皆さん、今まで、ありがとうございました。

Roy

Using FullCalendar in Rails

Fullcalendar is a powerful javascript calendar. It would be convenient if u wanna build some calendar-based service.

Using Fullcalendar is not very complex. First, u need to install FullCalendar in your Rails app.

gem 'fullcalendar-rails'
gem 'moment-rails'

and add following lines to your js

//= require moment
//= require fullcalendar

and css files

*= require fullcalendar

The basic usage is very easy, put

<div class="calendar"></div>

in html, and put following code in to coffee files(or put js version in js)

$ ->
    $(document).ready ->
        $('#calendar').fullCalendar {
              header: {
                left: 'prev,next today',
                center: 'title',
                right: 'month,agendaWeek,agendaDay'
              },
              monthNames: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
              monthNamesShort: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
              dayNames: ["日曜日", "月曜日", "火曜日", "水曜日", "木曜日", "金曜日", "土曜日"],
              dayNamesShort: ["日曜日", "月曜日", "火曜日", "水曜日", "木曜日", "金曜日", "土曜日"],  
            }
        return

FullCalendar uses EventObject to load events data and show on the calendar, EventObject is json format data loaded in js files, so here we need to load our Model data into FullCalendar.

Here I will how to load events in users views(a little more difficult than load in events views)

First, in models/event.rb, add

  def as_json(options = {})
    {
      :id => self.id,
      :title => self.name,
      :start => self.start_time,
      :end => self.end_time,
      :description => self.description,
      :allDay => self.is_all_day,
     }
     end

Here the code will tranfer event data in to json format, id, title, start, end, allDay is important, make sure in your model have these datas.

Then in controller/users_controller.rb

  respond_to do |format|
    format.html
    format.json { render json:@events.to_json }
  end

render the @events objects into json format, so now this json url should be like localhost:3000/users/(:id).json

The final step is to load json in js

events: window.location.href + '.json'

Use window.location.href to get current url, plus ".json", hmmm, it's a lazy way haha.

Till now, the FullCalendar would work for you :)

Origin Post