> Plugins are deprecated. Write an engine (
> http://guides.rubyonrails.org/engines.html ) instead. Engines can (among
> other things have migrations)

Thank you. This is what I'm doing right now - creating an engine.


Could you please explain me (of course if you know) how this code in the
engine works. This is an example from Rails Engine tutorial
(http://edgeguides.rubyonrails.org/engines.html#using-a-class-provided-by-the-application):


attr_accessor :author_name
belongs_to :author, class_name: "User"

before_save :set_author

private
  def set_author
    self.author = User.find_or_create_by(name: author_name)
  end


I cannot understand how "author_name" parameter appears in the model.
"author_name" parameter doesn't belong to model. It is from params, from
a form. So somehow it has to be passed to the model. But how?

attr_accessor is not attr_accessible. Why is it there? What does it
mean?


The table "posts" for the model Post is as follows:
---------------------------------
| id | title | text | author_id |
---------------------------------


So, the new @post has to be created this way:

@author = User.new(...some params...)
@post = Post.new(:title => "title", :text => "text", :author_id =>
@author.id)
@post.save!

But there is some "set_author" method. What is a magic is it with that
"set_author" method and :author_name parameter?

-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/571de4a2c44b55c909286dc6d8cdd52e%40ruby-forum.com?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to