1

So everything was working earlier. I cut a branch and made some changes that I didn't think would affect this poster attribute but I got this error. I switched to master and did not merge anything, and this was working before I cut the branch I mention above. And I got this error also.

I'm not sure what happened, maybe I twitched and put in a keystroke somewhere but I can't see it.

It says the error is in my restaurants controller. I'm using the poster attribute in my show.index.html.

show.index.html:

<% @restaurant.reviews.reverse.each do |review| %>
posted by: <%= review.poster %><br />
written: <%= time_ago_in_words(review.created_at) %> ago<br />
Review:<br />
<%= review.article %><br /><br />
<% end %>

The method for review in my restaurants_controller.rb:

def review
  @restaurant.reviews << Review.create!(review_params) ## <= this is the offending piece of code
  redirect_to :action => "show", :id => params[:id]
end

The method for review_params in restaurants_controller.rb:

def review_params
  params.require(:review).permit(:poster, :article)
end

I'm not sure what changed to make me get unknown attribute but if someone could help I'd appreciate it.

Thanks!

James White
  • 535
  • 10
  • 24

1 Answers1

1

The problem is likely down to two potential issues:

  1. Your poster attribute is conflicting with your poster association
  2. You don't have a poster attribute

Let me explain:


Association

Upon first seeing your code, I would have presumed your poster "attribute" would have, in fact, been an ActiveRecord association. This means that if you call the poster method, it will access the data from the associated model:

#app/models/review.rb
class Review < ActiveRecord::Base
   #fields id | poster_id | title | body | created_at | updated_at
   belongs_to :poster, class_name: "User"
   delegate :name, to: :poster, prefix: true #-> poster_name
end

#app/models/user.rb
class User < ActiveRecord::Base
   has_many :reviews
end

This association will give you the ability to call the the following (considering your database is set up correctly):

<% @restaurant.reviews.reverse.each do |review| %>
    <%= review.poster_name %>
<% end %>

Attribute

In regards to your attribute issue - the bottom line is whether you have a column called poster in your Review model / table.

To give you a more detailed explanation, you must remember that Rails is an object-orientated framework (by virtue of being built on Ruby), which means that every time you perform any functionality with it, it will "build" objects for you. We see these objects as our Models, but in reality, they are just Ruby classes with "attributes" appended to them, depending on the database.

You can see this most evidently with this documentation - every time you initialize a Rails / Ruby class, it's actually "building" an object with a series of instance methods, which we come to know as its attributes:

enter image description here

This means that if you're receiving an "unknown attribute" error, it means that your Model cannot build the attributes you desire. The base definition for this is that you don't have the correct column in your database, but moreover, you may be able to resolve with a simple fix (hack).

I would strongly recommend creating a migration to populate a poster column in your db. However, to see if this is the problem or not, you may wish to use attr_accessor to create a "virtual attribute":

#app/models/review.rb
class Review < ActiveRecord::Base
   attr_accessor :poster #-> to test whether this will resolve the issue
end
Community
  • 1
  • 1
Richard Peck
  • 76,116
  • 9
  • 93
  • 147
  • 1
    Tell me the error. Means another part of your code has a problem – Richard Peck Sep 07 '14 at 13:13
  • 1
    `attr_accessor :poster` did work. I think this is going to be me not understanding git and db. I was going to not let people enter the poster on a review and tie the user authentication from devise to the review. So I added a migration and in the migration file: `remove_column :reviews, :poster`. Then db:migrate. Since I didn't merge that branch I thought I would still have the poster column in the database. But I don't do I? I need to get that back. I did run `rake db:migrate` my test branch thinking that would recreate but it didn't fix it. So not sure how to get poster back. – James White Sep 07 '14 at 13:23
  • `undefined method `poster' for #` For this line: `posted by: <%= review.poster %>
    `
    – James White Sep 07 '14 at 14:22
  • 1
    Did you change the models to those I outlined? – Richard Peck Sep 07 '14 at 15:18
  • Running the migration and adding poster column back in worked. – James White Sep 07 '14 at 22:53