Have been messing around with Rails and while going through Hartl's tutorial I was asked to do a project. Figured it was a good opportunity to deviate off of the path, even though I don't know enough ;) Hence why I'm having a few problems.
I have two models:
Foo
, which has parameter name and has_many FooStatus
FooStatus
, which has parameters Foo.name and content, belongs_to Foo
Hartl's tutorial uses accounts that you log into, thus the @foo
parameter is already bound and foo.id
would be readily available for use. Once authenticated, a user can create a status for any Foo
. In my HTTP request I am trying to send over foo.name
and foostatus.content
and have that information save to my DB.
My questions: Will these models store the appropriate relationship / link between the two objects in the db?
The line <% if @foo.foostatus.any? %>
is giving me the error
SQLite3::SQLException: no such column: foostatus.foo_id: SELECT 1 AS one FROM "foostatus" WHERE "foostatus"."foo_id" = ? ORDER BY created_at DESC LIMIT 1
so I'm assuming that the .any?
method is looking for foostatus.foo_id
, which I don't store in my table.
I had originally thought that I could say
foo = Foo.find_by_name(params[:name])
@foostatus = foo.foostatus.build(params[:content])
but reading about Strong Parameters threw me in a circle, in that this wouldn't be allowed.
You can specify which columns are to be used as foreign and primary key for association:
class Foo < ActiveRecord::Base
has_many :foo_statuses, foreign_key: :foo_name, primary_key: :name
end
class FooStatus < ActiveReocrd::Base
belongs_to :foo, foreign_key: :foo_name, primary_key: :name
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With