私はRubyonRailsから始めて、Facebookユーザーのようなものを製品に変換し、パーソナライズされたギフトリストを作成するアプリケーションを作成しています。
ええと、私には3つのモデルがあります:ユーザーと好きな製品です。したがって、それらは次のとおりです。
class Like < ActiveRecord::Base
belongs_to :user
has_one :product
attr_accessible :category, :created_time, :name
end
class Product < ActiveRecord::Base
belongs_to :user
has_one :like
attr_accessible :image_url, :price_max, :price_min, :product_name,
:url
end
class User < ActiveRecord::Base
has_many :likes, dependent: :destroy
has_many :products, through: :likes, dependent: :destroy
attr_accessible :email, :password, :password_confirmation, :remember_me,:first_name, :last_name, :gender, :facebook_username, :provider, :uid
end
また、好きなものを見つけて製品に変えるのに役立つ2つの宝石を使用しています。これには、「railsconsole」で使用する2つの方法があります。
#Facebook Client
client = FBGraph::Client.new(
client_id: 'client_id',
secret_id: 'secret_id',
token: 'token'
)
#Getting and Recording Likes
like = client.selection.me.likes.info!.data.data
like.sort_by{ |like| like.created_time.to_i }.reverse.each do |like|
case like.category
when 'Musician/band' then category = 'band'
...
else next
Like.create(category: category, created_time: like.created_time, name: like.name, user_id: current_user.id)
end
#Product API Client
buscape = Buscape.new(
app_id: 'app_id',
sandbox: true
)
#Getting product through likes, and recording
def find_for_products
likes.each do |like|
case like.category
when 'band' then
product = @buscape.products.where(keyword: URI.encode(like.name), categoryId: 2921, results: 1)['product']
...
else next
end
Product.create(
image_url:product['thumbnail']['url'],
price_max:product['priceMax'],
price_min:product['priceMin'],
product_name:product['productName'],
url:product['links']['link'][0]['url']
)
end
end
Railsコンソールを使用すると、Like.newとProduct.newを使用して製品やいいねを手動で登録し、「user.products」にアクセスして使用するユーザーに割り当てることができますが、それを実装する方法がわかりません。私のコントローラーで、ユーザーがボタンをクリックした瞬間にcurrent_userのようなものをキャッチし、製品を作成します。
誰か助けてもらえますか?