I’ve been working on this for awhile and have finally decided to ask
for a little guidance.I have a slight problem trying to save a
selection.
I have two models:
A “Posting” has_many “Categories”, and a “Category” belongs_to one
“Posting”. With that said, in the posting model I have “has_many
:categories” and within the category model I have “belongs_to
:posting”. That part I ‘believe’ is correct, however I very well could
be off on that.
The columns in the postings table:
±------------------
| Field
±-----------------
| id
| title
| body
| user_id
| category_id
| posting_status_id
| posting_image_id
| comment_id
| created_at
| updated_at
±------------------
The columns in the categories table:
±------------
| Field
±------------
| id
| title
| description
±------------
With the following code I can successfully display the categories in
the drop down, but the ID is not saved to the postings table.
I’ve been working on this for awhile and have finally decided to ask
for a little guidance.I have a slight problem trying to save a
selection.
I have two models:
A “Posting” has_many “Categories”, and a “Category” belongs_to one
“Posting”. With that said, in the posting model I have “has_many
:categories” and within the category model I have “belongs_to
:posting”. That part I ‘believe’ is correct, however I very well could
be off on that.
The columns in the postings table:
±------------------
| Field
±-----------------
| id
| title
| body
| user_id
| category_id
| posting_status_id
| posting_image_id
| comment_id
| created_at
| updated_at
±------------------
The columns in the categories table:
±------------
| Field
±------------
| id
| title
| description
±------------
With the following code I can successfully display the categories in
the drop down, but the ID is not saved to the postings table.
The AR convention is that the table with the foreign key (in this case,
the postings table) “belongs_to” the table it references.
The way you have it defined, it should be…
class Posting <AR::Base
belongs_to :category
end
“posting belongs to a category” <- that makes sense, right?
class Category < AR::Base
has_many :postings
end
“category has many postings” <- this too.
I would have to see your create action to know why it isn’t saving.
Having the associations backwards is probably causing at least some of
your problems.
I was trying to call the category object instead of the posting
object, which is why the value was always null when it was saved.
Stupid little mistakes!
Thanks for your help on the associations though. I had those kind of
messed up as well.
I did in fact have the assoications backwards. Doh! However, the
create action is still not saving the ID correctly. Here’s what I have
in my create action:
def create @posting = Posting.new(params[:posting]) @posting.user_id = session[:user_id]
if @posting.save
flash[:notice] = ‘Posting was successfully created.’
redirect_to :action => ‘list’
else
render :action => ‘new’
end
end
I checked the log files and it looks as though the correct parameters
are being passed: