I’m just start to develop on Ruby on Rails and i’ve question about complex model relation.
I would like to know how i can have the user information when i request news. How do I create the associated models that make this possible?
-- USER --
t.integer :uid
t.string :username
t.string :password
t.string :email
t.string :firstname
t.string :lastname
t.integer :promo
t.integer :access_level
t.datetime :birthday
t.datetime :last_login
-- NEWS --
t.integer :news_id
t.integer :writter_id
t.datetime :date
t.string :title, :limit => 50
t.string :category, :limit => 10
t.text :content
t.valided :boolean
t.onscreen :boolean
So, i request new by using :
def self.getLastNews(max_entry = 5)
nws = News.order(:date).limit(max_entry).map
end
Tarscher is right, you are making your life difficult…
Your id’s should just be id in each model.
if the you would have a foreign key in the news model called user_id, or writer_id, but you would then need to add the assoiation on the news model as:
then to include all the users when you get the news model you would call the news like this:
but really you should try to follow the rails conventions as it will make your life easier… you don’t have to, it’s very flexible, but the framework is based on convention over configuration.