I am trying to save a person's Facebook friends into my database. I want to store the Facebook users in a table and then store their friendships in another table. The friendships would have the integer of the FacebookUser that requested the friendship and the integer of the friend, both of which are foreign keys to the facebook_users table. However I keep getting this message when I try to link the a user's facebook friends with friendships.
Error
ActiveRecord::HasManyThroughSourceAssociationNotFoundError: Could not find the source association(s) :friend or :friends in model Friendship. Try 'has_many :friends, :through => :friendships, :source
=> <name>'. Is it one of :FacebookUser or :FacebookFriend?
friendship.rb
class Friendship < ActiveRecord::Base
attr_accessible :facebook_user_id, :facebook_friend_id
belongs_to :FacebookUser
belongs_to :FacebookFriend, :class_name => :FacebookUser
end
facebook_user.rb
class FacebookUser < ActiveRecord::Base
attr_accessible :first_name, :gender, :last_name
has_many :friendships, :foreign_key => :facebook_user_id
has_many :friends, :through => :friendships, :source => :FacebookUser
end
Schema
create_table "facebook_users", :force => true do |t|
t.string "first_name"
t.string "last_name"
t.string "gender"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "friendships", :force => true do |t|
t.integer "facebook_user_id"
t.integer "facebook_friend_id"
end
the convention Rails uses is to use associations as defined by the class name and the foreign key. if you've set up your tables like above, you should change your models to the following.
class Friendship < ActiveRecord::Base
attr_accessible :facebook_user_id, :facebook_friend_id
belongs_to :facebook_user # implies a foreign key of facebook_user_id and class of FacebookUser
belongs_to :facebook_friend, class_name: 'FacebookUser' #implies a foreign key of facebook_friend_id
end
class FacebookUser < ActiveRecord::Base
attr_accessible :first_name, :gender, :last_name
has_many :friendships
has_many :friends, :through => :friendships, :source => :facebook_friend
end
Related
I have created a BETA Invitation system by following this tutorial. http://railscasts.com/episodes/124-beta-invitations. Users can also follow one another in my Rails app.
How can I have the Invitee follow the person who Invited him on sign up?
Currently, I am trying to establish this in my User Model using a method, but I am having trouble creating the method that allows the Invitee to follow the Inviter via sender_id/user_id.
This is the code I have used so far.
SCHEMA
create_table "users", :force => true do |t|
t.string "name"
t.string "email"
t.integer "invitation_id"
t.integer "invitation_limit"
t.timestamp "created_at", :null => false
t.timestamp "updated_at", :null => false
t.string "password_reset_token"
t.timestamp "password_reset_sent_at"
end
create_table "invitations", :force => true do |t|
t.integer "sender_id"
t.string "recipient_email"
t.string "token"
t.datetime "sent_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
MODELS
USER
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation, :invitation_token
has_many :relationships, foreign_key: "follower_id", dependent: :destroy
has_many :followed_users, through: :relationships, source: :followed
has_many :reverse_relationships, foreign_key: "followed_id",
class_name: "Relationship",
dependent: :destroy
has_many :followers, through: :reverse_relationships, source: :follower
has_many :sent_invitations, :class_name => 'Invitations', :foreign_key => 'sender_id'
belongs_to :invitation
after_create :follow_inviter #---------- HERE!!
def follow_inviter #---------- HERE!!
inviters = Invitation.find_by_sender_id
inviters.each do |invite|
self.follow!(invite)
end
end
def invitation_token
invitation.token if invitation
end
def invitation_token=(token)
self.invitation = Invitation.find_by_token(token)
end
def following?(other_user)
relationships.find_by_followed_id(other_user.id)
end
def follow!(other_user)
relationships.create!(followed_id: other_user.id)
end
def unfollow!(other_user)
relationships.find_by_followed_id(other_user.id).destroy
end
end
RELATIONSHIPS
class Relationship < ActiveRecord::Base
attr_accessible :followed_id
belongs_to :follower, class_name: "User"
belongs_to :followed, class_name: "User"
validates :follower_id, presence: true
validates :followed_id, presence: true
end
INVITATION
class Invitation < ActiveRecord::Base
attr_accessible :recipient_email, :sender_id, :sent_at, :token
belongs_to :sender, :class_name => "User"
has_one :recipient, :class_name => "User"
VALID_EMAIL_REGEX = /\A[\w+\-.]+#[a-z\d\-.]+\.[a-z]+\z/i
before_create :generate_token
private
def generate_token
self.token = Digest::SHA1.hexdigest([Time.now, rand].join)
end
end
This should work.
def follow_inviter
if invitation = Invitation.find_by_recipient_email(email)
follow!(invitation.sender)
end
end
But your models associations are not well defined. For example has_one :recipient, :class_name => "User" in the Invitation expects that there is a recipient_id in the table, but is not. You should review that.
I might be wrong, I'm a junior rails developer, but it looks like where you have inviters = Invitation.find_by_sender_id you should have something like this inviters = Invitation.find_by_sender_id(id_of_sender) where id_of_sender is the id of the person who sent the invitation.
The find_by_sender_id takes 1 argument (the ID of the sender to be found) and that is why you get the error wrong number of arguments (0 for 1).
Also for what it's worth, I'm pretty sure the find_by_*_id methods, where * is a model in your database, are deprecated. Rails 4 uses something like Invitation.find(id_of_sender). When you call the find method on an active record model, it takes an id as a parameter.
You can also use Invitation.find_by(email: 'user#example.com') to find records based on any property you give them.
Apologies if I am misunderstanding your question... I have a similar requirement, where one user can mark another as a 'favorite' user (i.e. users table has a self-relationship)
To implement this, I added a table called user_favorite like this:
db\schema.rb
create_table "user_favorites", :id => false, :force => true do |t|
t.integer "user_id"
t.integer "favorite_user_id"
end
add_index "user_favorites", ["user_id"], :name => "index_user_favorites_on_user_id"
app\models\user.rb
class User < ActiveRecord::Base
has_and_belongs_to_many :favorite_users, :class_name => 'User', :join_table => "user_favorites", :foreign_key => "user_id", :association_foreign_key => "favorite_user_id"
def is_favorite?(user)
self.favorite_users.include?(user)
end
def toggle_favorite(favorite_user)
if favorite_user
if self.favorite_users.include?(favorite_user)
self.favorite_users.delete favorite_user
else
self.favorite_users << favorite_user
end
end
end
end
So I am developing a rails app that will have two kinds of Users, student/tutor. but I only have one User model (using cancan for auth), so when I try to set up the meeting model (which has one tutor and one student) how do I do this? This is the model:
class Meeting < ActiveRecord::Base
belongs_to :student
belongs_to :tutor
attr_accessible :price, :subject, :time
end
and here's the relevant part of the schema:
create_table "meetings", :force => true do |t|
t.string "subject"
t.integer "student_id"
t.integer "tutor_id"
t.datetime "time"
t.integer "price"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "meetings", ["student_id"], :name => "index_meetings_on_student_id"
add_index "meetings", ["tutor_id"], :name => "index_meetings_on_tutor_id"
Without having to have two extra models containing student and tutor can I use those labels?
one way to do it..
class Meeting < ActiveRecord::Base
belongs to :student, class_name: 'User'
belongs to :tutor, class_name: 'User'
class User < ActiveRecord::Base
has_many :meet_with_students, class_name: 'Meeting', foreign_key: :tutor_id
has_many :students, through: :meet_with_students, source: :student
has_many :meet_with_tutors, class_name: 'Meeting', foreign_key: :student_id
has_many :tutors, through: :meet_with_tutors:, source: :tutor
I think you're looking for class_name:
class Meeting < ActiveRecord::Base
belongs_to :student, class_name: "User"
belongs_to :tutor, class_name: "User"
end
I have a many-to-many association between a Post and a Tag model:
post.rb:
has_many :taggings, dependent: :destroy
has_many :tags, through: :taggings
tag.rb:
has_many :taggings, :dependent => :destroy
has_many :posts, :through => :taggings
tagging:
attr_accessible :tag_id, :post_id
belongs_to :post
belongs_to :tag
I want to have a page where I list all the tags and how many posts each tag has.
So I added a posts_count column to tags:
create_table "tags", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "posts_count", :default => 0, :null => false
end
I've used counter cache before:
reply.rb:
belongs_to :post, :counter_cache => true
But I'm not sure how to do it with this many-to-many association. Any ideas?
Use common :counter_cache option for tags. Despite the fact that it counts Taggings objects, which belongs_to (just one) post, this is what you looking for.
# tagging:
attr_accessible :tag_id, :post_id
belongs_to :post
belongs_to :tag, :counter_cache => :posts_count
validates_uniqueness_of :tag_id, :scope => :post_id
Validator will prevent the creation of several identical tags for the same posts, thus you can avoid duplicate records.
I am linking a Company and User model through an Association table:
class User < ActiveRecord::Base
has_many :associations
has_many :companies, :through => :associations
class Company < ActiveRecord::Base
has_many :associations
has_many :users, :through => :associations
In my Association I have an boolean column named active:
create_table "associations", :id => false, :force => true do |t|
t.integer "company_id"
t.integer "user_id"
t.boolean "active"
In my company controller I am trying to get a company array that includes the fact that the user has been activated or not for the company.
I managed to use a scope, but this only achieved to filter if the company is active or not, and not the association:
#companiesactive = #user.companies.by_status(true)
Looking to get the result of something like:
#companiesuseractive = #user.companies.where("association.active", true)
Any help would be much appreciated.
In my Rails 3 project, I have a user model with a self referential join, through the follow model. I want to use this join table to find activity related to the followed user. I have almost everything set up correctly, except that the query generated by the join is totally ignoring the :primary_key option on the join model.
Here is the relevant schema for the relevant models:
create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "first_name"
t.string "last_name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "follows", :force => true do |t|
t.integer "user_id"
t.integer "followed_user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "activities", :force => true do |t|
t.integer "user_id"
t.text "body"
t.datetime "created_at"
t.datetime "updated_at"
end
Here's the associations in the models
class User < ActiveRecord::Base
has_many :follows
has_many :followed_users, :through => :follows
has_many :followed_activities, :through => :follows
has_many :activities
end
class Follow < ActiveRecord::Base
belongs_to :user
belongs_to :followed_user, :class_name => "User"
has_many :followed_activities, :primary_key => :followed_user, :foreign_key => :user_id, :class_name => "Activity"
end
The following work just fine:
u = User.first
u.follows # returns corresponding records from the follows table
u.followed_users # returns all users that u is following
u.followed_users.first.activities # returns all activity records corresponding to the first person the user is following
Follow.first.activities # same as the previous
However, the following just returns an empty array:
u.followed_activities
Here is the sql that is generated from the last statement:
Activity Load (0.2ms) SELECT `activities`.* FROM `activities` INNER JOIN `follows` ON `activities`.user_id = `follows`.id WHERE ((`follows`.user_id = 1))
The reason it isn't working is because it is trying to join use 'follows'.id as the primary key rather than 'follows'.followed_user.
Is this a bug, or do I have to repeat the :primary_key declaration somewhere on the user model? I can't find any mention anywhere in the Rails api, or anywhere else online.
Rails Version: 3.0.7
I've found it intuitive to daisy chain relationships with the 'nested_has_many_through' gem, http://rubygems.org/gems/nested_has_many_through which will be a standard part of rails 3.1 and could give you another tool to tackle the issue here
It will let you do something like this:
class Author < User
has_many :posts
has_many :categories, :through => :posts, :uniq => true
has_many :similar_posts, :through => :categories, :source => :posts
has_many :similar_authors, :through => :similar_posts, :source => :author, :uniq => true
has_many :posts_of_similar_authors, :through => :similar_authors, :source => :posts, :uniq => true
has_many :commenters, :through => :posts, :uniq => true
end
class Post < ActiveRecord::Base
belongs_to :author
belongs_to :category
has_many :comments
has_many :commenters, :through => :comments, :source => :user, :uniq => true
end
This has super-simplified my queries and collections. I hope you find an answer to your problem, it's a tough one!
Justin, you have 2 associations called "followed_activities". sure, they have different context (different models), but I'd like to ask you to try method inside the association block like this:
has_many :followed_users, :through => :follows do
def activities
end
end