Dienstag, 24. Juni 2008

STUCK things

Associations.
==========

What i want to do....

current_user.following_users.messages

This should deliver a bunch of messages from all the users that the current user is following (a la twitter).

I can't get it to work for the life of me.

################################Here the migrations.

User

create_table "users", :force => true do |t|
t.column :login, :string
t.column :email, :string
t.column :crypted_password, :string, :limit => 40
t.column :salt, :string, :limit => 40
t.column :created_at, :datetime
t.column :updated_at, :datetime
t.column :remember_token, :string
t.column :remember_token_expires_at, :datetime
t.column :activation_code, :string, :limit => 40
t.column :activated_at, :datetime
end


Message

create_table :messages do |t|
t.string :message
t.integer :user_id
t.integer :movie_id
t.timestamps
end


Following

create_table :followings do |t|
t.integer :follower_user_id
t.integer :following_user_id

t.timestamps
end

#############################Here the Models

User

class User < foreign_key =""> :following_user_id, :class_name => "Following"
has_many :follower_users,:foreign_key => :follower_user_id, :class_name => "Following"
has_many :messages
has_many :messages, :through => :follower_users,:foreign_key => :follower_user_id, :class_name => "Following"
has_many :movies, :through => :messages

Message

class Message < foreign_key =""> :following_user_id, :class_name => "Following"
# has_many :follower_users,:foreign_key => :follower_user_id, :class_name => "Following"
end

Following

class Following < foreign_key =""> :follower_user_id, :class_name => "User" #id of the user that is following
belongs_to :following_user, :foreign_key => :following_user_id, :class_name => "User" #id of the user that is being followed
end


+++++++++++++++++++++ my solution so far

@user=User.find_by_id(params[:user_id])
@following_users=@user.followings.each { |follow| follow.user_id }
@messages=Message.find_all_by_user_id(@following_users)

Refactoring
=========

def get_page_count
results = @doc.search("//td[@class='resultCount']/").first.to_s.gsub(/\./,"")
tmp_array=results.scan(/(\d+)-(\d+).*?(\d+)/)
myarray=tmp_array[0]
@max_pages_in_category = 1
if !myarray.nil?
@max_pages_in_category= (myarray[2].to_i / 12) # total number of items / itmes on a page
if (myarray[2].to_i % 12) != 0 then @max_pages_in_category+=1 end
@total_articles_in_category=myarray[2];
end
end


def render_products
titles = @doc.search("//span[@class='srTitle']/")
titles.each do |title|
print "."
string=title.to_s
string.gsub!(/\(.+?\)/,"")
string.gsub!(/\[.+?\]/,"")
if @data_file!=0 : @data_file.puts @tags+":"+string end
end
end


Keine Kommentare: