我从 Ruby on Rails 开始,并正在制作一个应用程序,该应用程序将 facebook 用户的喜好转换为产品,创建个性化的礼物清单。
好吧,我有三个模型:用户和喜欢的产品。它们是这样的:
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
我还使用了两个宝石来帮助我抓住喜欢并将它们变成产品。为此,我在“rails 控制台”中使用了两种方法:
#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 之类的内容并在用户单击“为我查找产品”按钮时生产产品
谁能帮我?